Last modified: Jan 10, 2023 By Alexander Williams

remove the last word from string Python

to remove the last word from string we need to follow these steps

#1.split string

#2.remove the last item of list

#3. convert list to string

example

1
2
3
4
5
6
7
8
9
string = "hello world"
#split string
spl_string = string.split()
#remove the last item in list
rm = spl_string[:-1]
#convert list to string
listToStr = ' '.join([str(elem) for elem in rm])
#print string
print(listToStr)

output

1
hello