remove last word in a string in python

Python – Remove the Last Word From String

In this tutorial, we will look at how to remove the last word from a string in Python with the help of some examples.

remove last word in a string in python

You can use the string split() function to remove the last word from a string. The split() function is generally used to split a string into its constituent words based on a delimiter, which, by default, is a whitespace character. For example –

# create a string
s = "Babbling, bumbling band of baboons"
# split the string
print(s.split())

Output:

['Babbling,', 'bumbling', 'band', 'of', 'baboons']

You can see that using the string split() function on the above string resulted in a list of words. You can now slice this list of words to remove the last word and reconstruct a sentence from the remaining words using the string join() function.

# create a string
s = "Babbling, bumbling band of baboons"
# split the string
words = s.split()
# remove last word and join the remaning words
print(" ".join(words[:-1]))

Output:

Babbling, bumbling band of

The resulting string does not have the last word from the original string.

For more on the string split() function, refer to its documentation.

Alternatively, you can use the slice operation to remove the last word from the string.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

For this, we slice the string from its start to the starting index of the last word inside the string and then apply the string rstrip() function to remove any whitespaces in the resulting string’s end.

Using a negative index can be helpful here. We would use the negative length of the last word as the negative index to get its position inside the string. Here’s an example –

# create a string
s = "Babbling, bumbling band of baboons"
# remove the last word
print(s[:-7].rstrip())

Output:

Babbling, bumbling band of

We get the same result as above. Here we had to explicitly provide the length of the last word, 7 in the slice operation in order to omit it in the resulting string.

For this method to work, you need to know the length of the last word in advance. If that is not the case, you might have to use the string split() function. Thus, it is recommended that you use the string split() method.

You might also be interested in –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top