Horje
select first word in string python Code Example
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']
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()




Python

Related
pandas set options Code Example pandas set options Code Example
axis number size matplotlib Code Example axis number size matplotlib Code Example
python alphabet capital Code Example python alphabet capital Code Example
model pickle file create Code Example model pickle file create Code Example
pip pickle Code Example pip pickle Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7