Remove the first word from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 18, 2022

banner

Photo from Unsplash

Remove the first word from a String in Python #

To remove the first word from a string:

  1. Use the str.split() method to split the string on a space, a single time.
  2. Access the second list item.
  3. The second list item consists of the remainder of the string.
main.py
my_str = 'one two three four five' # ✅ Remove the first word from a string (str.split()) new_str = my_str.split(' ', 1)[1] print(new_str) # 👉️ 'two three four five' # ----------------------------- # ✅ Remove the first word from a string (re.partition()) new_str = my_str.partition(' ')[2] print(new_str) # 👉️ 'two three four five'

The first example uses the str.split() method to remove the first word from a string.

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)
If the separator is not found in the string, a list containing only 1 element is returned.

We split the string on a space, a single time.

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

The first list item contains the first word in the string and the second contains the remainder of the string.

If the string contains a single word, we'd get an error because we'd try to access the list at an index out of range.

main.py
my_str = 'one' # ⛔️ IndexError: list index out of range new_str = my_str.split(' ', 1)[1] print(new_str) # 👉️ 'two three four five'

If you need to handle this scenario, use a try/except block.

main.py
my_str = 'one' print(my_str.split(' ')) # 👉️ ['one'] try: new_str = my_str.split(' ', 1)[1] print(new_str) # 👉️ 'two three four five' except IndexError: # 👇️ this runs pass

The string doesn't contain a space, so splitting it on a space returns a list with a single item.

The IndexError is caught and handled by the except block.

Alternatively, you can use the str.partition() method.

Remove the first word from a String using str.partition() #

To remove the first word from a string:

  1. Use the str.partition() method to split the string on a space a single time.
  2. Access the third list item.
  3. The third list item consists of the remainder of the string.
main.py
my_str = 'one two three four five' new_str = my_str.partition(' ')[2] print(new_str) # 👉️ 'two three four five'

The str.partition method splits the string at the first occurrence of the provided separator.

main.py
my_str = 'one two three four five' # 👇️ ('one', ' ', 'two three four five') print(my_str.partition(' '))

The method returns a tuple containing 3 elements - the part before the separator, the separator, and the part after the separator.

If the separator is not found in the string, the method returns a tuple containing the string, followed by 2 empty strings.

This is quite useful because the tuple is always going to contain 3 elements, even if the string is empty or contains a single word.

This means that this approach won't raise an IndexError, so a try/except statement is not needed.

main.py
my_str = 'one' # 👇️ ('one', '', '') print(my_str.partition(' ')) new_str = my_str.partition(' ')[2] print(repr(new_str)) # 👉️ ''

Even though the string in the example doesn't contain any spaces, the str.partition method still returns a tuple containing 3 elements.

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.