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 :: import datetime 
Python :: python iterate directory 
Python :: python time code 
Python :: install streamlit 
Python :: tkinter label border 
Python :: python pygame screen example 
Python :: EnvironmentError command line 
Python :: download from url using urllib python 
Python :: create dictionary python from two lists 
Python :: pandas rename specific column 
Python :: The specified device is not open or is not recognized by MCI. 
Python :: replace all spacec column with underscore in pandas 
Python :: requests download image 
Python :: django model specify table name 
Python :: format to 2 or n decimal places python 
Python :: convert date time to date pandas 
Python :: show image in tkinter pillow 
Python :: how to print hello world 10 times in python 
Python :: terminal python version 
Python :: python calculate time taken 
Python :: how to convert datetime to jdatetime 
Python :: pytube urllib.error.HTTPError: HTTP Error 410: Gone 
Python :: sort python nested list according to a value 
Python :: finding duplicate characters in a string python 
Python :: classification report scikit 
Python :: import csv file using pandas 
Python :: Calculate median with pyspark 
Python :: array of 1 to 100 python 
Python :: how to limit a command to a permission in discord.py 
Python :: image in cv2 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =