To remove the last word from string we need to follow these steps
- convert the string to list by using split() function
- remove the last item from the list
- convert the list to string
example
- #string
- 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
hello
Thank you, for your example.