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 144 views
1 answer 163 views
1 answer 140 views
3 answers 185 views
1 answer 147 views
1 answer 189 views
1 answer 156 views
...