Python | Removing Initial word from string
During programming, sometimes, we can have such a problem in which it is required that first word from the string has to be removed. These kind of problems are common and one should be aware about the solution for such problems. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using split()
This task can be performed using the split function which performs a split of words and separates the first word of string with the entire words.
# Python3 code to demonstrate working of # Removing Initial word from string # Using split() # initializing string test_str = "GeeksforGeeks is best" # printing original string print("The original string is : " + test_str) # Using split() # Removing Initial word from string res = test_str.split(' ', 1)[1] # printing result print("The string after omitting first word is : " + str(res)) |
The original string is : GeeksforGeeks is best The string after omitting first word is : is best
Method #2 : Using partition()
The partition function is used to perform this particular task in comparatively lesser internal task as compared to function used in above method.
# Python3 code to demonstrate working of # Removing Initial word from string # Using partition() # initializing string test_str = "GeeksforGeeks is best" # printing original string print("The original string is : " + test_str) # Using partition() # Removing Initial word from string res = test_str.partition(' ')[2] # printing result print("The string after omitting first word is : " + str(res)) |
The original string is : GeeksforGeeks is best The string after omitting first word is : is best
Recommended Posts:
- Python | Removing unwanted characters from string
- Python program for removing i-th character from a string
- Find frequency of each word in a string in Python
- Find the first repeated word in a string in Python using Dictionary
- Count occurrences of a word in string
- Python | Removing strings from tuple
- Python | Removing first element of list
- Python | Removing dictionary from list of dictionaries
- Python | Removing duplicate dicts in list
- Removing stop words with NLTK in Python
- Python | Print list after removing element at given index
- Python | Word Stretch
- Generating Word Cloud in Python | Set 2
- Python | Word Similarity using spaCy
- Python | Word Embedding using Word2Vec
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



