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

3 Answers

0 votes
s = "python"
  
last3Characters = s[-3:]
  
print(last3Characters)
  
  
  
  
'''
run:
  
hon
 
'''

 



answered Nov 3, 2021 by avibootz
0 votes
s = "python"

n = 3  
last3Characters = s[-n:]
  
print(last3Characters)
  
  
  
  
'''
run:
  
hon
 
'''

 



answered Nov 3, 2021 by avibootz
0 votes
s = "python"

n = 3  
last3Characters = s[len(s) - n:]
  
print(last3Characters)
  
  
  
  
'''
run:
  
hon
 
'''

 



answered Nov 3, 2021 by avibootz

Related questions

2 answers 175 views
1 answer 107 views
1 answer 111 views
1 answer 99 views
1 answer 124 views
1 answer 124 views
1 answer 115 views
...