Answers for "choosing first letter of a string element python"

4

select first word in string python

name = "Rick Sanchez"
first_name = name.split(' ').pop(0)
print(first_name)
#Output
'Rick'

#BONUS
#From strings in a list (using list comprehension)
names = ["Rick Sanchez", "Morty Smith", "Summer Smith", "Jerry Smith", "Beth Smith"]
first_names = [name.split(' ').pop(0) for name in names]
print(first_names)
#Output
['Rick', 'Morty', 'Summer', 'Jerry', 'Beth']
Posted by: Guest on August-21-2021

Code answers related to "choosing first letter of a string element python"

Python Answers by Framework

Browse Popular Code Answers by Language