Answers for "how to return the first letter of a word in 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
4

print first word of a string python and return it

def print_first_word():
    words = "All good things come to those who wait"
    print(words.split().pop(0))
    #to print the last word use pop(-1)
print_first_word()
Posted by: Guest on April-07-2020

Code answers related to "how to return the first letter of a word in python"

Python Answers by Framework

Browse Popular Code Answers by Language