remove first word from string in python

Python – Remove the First Word From String

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

remove first word from string in python

You can use the string split() function to remove the first 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 = "You are a wizard Harry!"
# split the string
print(s.split())

Output:

['You', 'are', 'a', 'wizard', 'Harry!']

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 first word and reconstruct a sentence from the remaining words using the string join() function.

# create a string
s = "You are a wizard Harry!"
# split the string
words = s.split()
# remove first word and join the remaning words
print(" ".join(words[1:]))

Output:

are a wizard Harry!

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

You can also accomplish the above task using just the string split() function. For this, pass an additional parameter, maxsplit = 1 to the split() function. This parameter indicates the number of splits to make.

# create a string
s = "You are a wizard Harry!"
# split the string
print(s.split(maxsplit=1))

Output:

📚 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.

['You', 'are a wizard Harry!']

You can see that it results in a list of just two strings from splitting the original string just once (this split is done on the first occurrence of the delimiter, a whitespace character in our case).

Now, we can take the value at index 1 from the above list to get our resulting string with the first word removed. Here’s the complete code –

# create a string
s = "You are a wizard Harry!"
# remove the first word
print(s.split(maxsplit=1)[1])

Output:

are a wizard Harry!

The first word is removed.

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

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

For this, we slice the string from the length of the first word to the end of the string and then apply the string lstrip() function to remove any whitespaces in the string start after the slice operation.

# create a string
s = "You are a wizard Harry!"
# remove the first word
print(s[3:].lstrip())

Output:

are a wizard Harry!

We get the same result as above.

For this method to work, you need to know the length of the first 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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


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