Remove the first and last words from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 19, 2022

banner

Photo from Unsplash

Remove the first and last words from a String in Python #

To remove the first and last words from a string:

  1. Use the index() and rindex() methods to get the indexes of the first and last spaces.
  2. Use string slicing to get a slice after the first space up to the last space.
  3. The new string won't contain the first and last words of the original string.
main.py
my_str = 'one two three four' # ✅ Remove first and last words from string (string slicing) new_str = my_str[my_str.index(' ') + 1:my_str.rindex(' ')] print(new_str) # 👉️ 'two three' # ------------------------------------------------ # ✅ Remove first and last words from string (split() and join()) new_str = ' '.join(my_str.split(' ')[1:-1]) print(new_str) # 👉️ 'two three' # ------------------------------------------------ # ✅ Remove first word from string (str.split()) without_first = my_str.split(' ', 1)[1] print(without_first) # 👉️ 'two three four' # ------------------------------------------------ # ✅ Remove last word from string (str.rsplit()) without_last = my_str.rsplit(' ', 1)[0] print(without_last) # 👉️ 'one two three'

The first example uses string slicing to remove the first and last words from the string.

main.py
my_str = 'one two three four' new_str = my_str[my_str.index(' ') + 1:my_str.rindex(' ')] print(new_str) # 👉️ 'two three'

The str.index method returns the index of the first occurrence of the provided substring in the string.

The str.rindex method returns the highest index in the string where the provided substring is found.

The methods raise a ValueError if the substring is not found in the string.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

We added 1 to the index of the first space in the string because we want to exclude it from the result.

The stop index is exclusive (up to, but not including), so we used the output of the rindex() method.

Alternatively, you can use the str.split() and str.join() methods.

main.py
my_str = 'one two three four' new_str = ' '.join(my_str.split(' ')[1:-1]) print(new_str) # 👉️ 'two three'

The first step is to use the str.split() method to split the string on each space.

main.py
my_str = 'one two three four' # 👇️ ['one', 'two', 'three', 'four'] print(my_str.split(' '))

We used list slicing to exclude the first and last items from the list.

The slice my_list[1:-1] starts at the second list item and goes up to, but not including the last list item.

main.py
my_str = 'one two three four' # 👇️ ['two', 'three'] print(my_str.split(' ')[1:-1])
Negative indices can be used to count backwards, e.g. my_list[-1] returns the last item in the list and my_list[-2] returns the second-to-last item.

The stop index is exclusive, so the last list item is not contained in the result.

The last step is to use the str.join() method to join the remainder of the list into a string.

main.py
my_str = 'one two three four' new_str = ' '.join(my_str.split(' ')[1:-1]) print(new_str) # 👉️ 'two three'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

For our purposes, we joined the elements in the list with a space separator.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.