Answers for "how to get the first word from a string 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
10

python get first character of string

string = 'This is a string'
print(string[0])
#output: 'T'
Posted by: Guest on April-18-2020

Code answers related to "how to get the first word from a string in python"

Python Answers by Framework

Browse Popular Code Answers by Language