How to get the last N characters of a string in Python

3 Answers

0 votes
s = "file.log"

print(s[-4:])



'''
run:
  
.log
  
'''

 



answered Nov 16, 2019 by avibootz
0 votes
s = "file.log"

N = -4

print(s[N:])



'''
run:
  
.log
  
'''

 



answered Nov 16, 2019 by avibootz
0 votes
s = "python java php c++"
 
N = 3
last_n_chars = s[-N:]
 
print(last_n_chars)
 
 
 
 
'''
run:
 
c++
 
'''

 



answered Apr 17, 2024 by avibootz

Related questions

1 answer 159 views
1 answer 181 views
1 answer 157 views
3 answers 205 views
1 answer 164 views
1 answer 201 views
1 answer 169 views
...