Remove First Word From String in Python

Remove First Word From String in Python | Sometimes we will have to remove some words from the string, so to achieve this easily we can use python functions, there are python built-in functions that help to remove the words from the string. The problem is to remove the first word from the string see the below example to understand in detail.

Example:-

String = “Python Programming
After the removal of the first word: “Programming”

We can do this by two methods:-

  1. Using partition() method
  2. Using split() method

Let us see these two methods in detail. Below are the method details of split() and partition() methods.

String.split(seperator, maxsplit)

separator:- This specifies the separator to use while splitting, this is an optional field. The default value is whitespace.

maxsplit:- This is also an optional field, that specifies how many splits to perform. -1 is the default value which is all the occurrences.

String.partition(value)

value:- This specifies the substring got to be searched. This is a required field.

Remove First Word From String in Python using partition()

# Python program to remove first word from string

tst_str = "Know Program"
print("The original string: " + tst_str)

result = tst_str.partition(' ')[2]
print("The string after omitting" +
      " first word is : " + str(result))

Output:-

The original string: Know Program
The string after omitting first word is : Program

The partition() method takes space as a parameter. And as soon as it finds the space, it splits the string.

Python Program to Remove First Word From String using split()

# Python program to remove first word from string

tst_str = "Know Program "
print("The original string: " + tst_str)

result = tst_str.split(' ', 1)[1]
print("The string after omitting " +
      "first word is : " + str(result))

Output:-

The original string: Know Program
The string after omitting first word is : Program

The split() method takes the whitespace and 1 as a parameter. And as soon as it finds the whitespace it splits the substring once.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *