Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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']
Comment

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()
Comment

PREVIOUS NEXT
Code Example
Python :: mediana python 
Python :: inverse matrix python numpy 
Python :: list comprehension 
Python :: string remove in python 
Python :: open csv from url python 
Python :: random 2 n program in python 
Python :: How to append train and Test dataset in python 
Python :: timer 1hr 
Python :: django messages 
Python :: relativefrequencies of the unique values pandas 
Python :: stack concatenate dataframe 
Python :: make button bigger tkinter with grid 
Python :: drop colums whoose value are object type in python 
Python :: group by pandas count 
Python :: run python script every hour 
Python :: match statement 
Python :: python cache 
Python :: input in one line python 
Python :: get last n in array python 
Python :: jupyter dark theme vampire 
Python :: pandas rename column by dictionary 
Python :: pandas get group 
Python :: python simple web app 
Python :: python program to check if binary representation is a palindrome 
Python :: feature selection python 
Python :: planets python 
Python :: setattr python 
Python :: Sum items in a list with ints and strings in python 
Python :: link in embed discord.py 
Python :: how to repeat if statement in python 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =