How to get the first three characters of a string in Python

1 Answer

0 votes
def first_three(str):
	return str[:3] if len(str) > 3 else str

print(first_three('python'))
print(first_three('abc'))
print(first_three('xy'))


  
  
'''
run:
  
pyt
abc
xy
 
'''

 



answered Sep 1, 2021 by avibootz

Related questions

1 answer 225 views
1 answer 130 views
1 answer 110 views
1 answer 137 views
...