Python | Removing Initial word from string
Last Updated :
24 Apr, 2023
During programming, sometimes, we can have such a problem in which it is required that the first word from the string has to be removed. These kinds of problems are common and one should be aware about the solution to such problems. Let's discuss certain ways in which this problem can be solved.
Method #1: Using split() Method
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
# 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))
Output : The original string is : GeeksforGeeks is best
The string after omitting first word is : is best
Time complexity: O(1)
Auxiliary space: O(1)
Method #2: Using partition() Method
The partition function is used to perform this particular task in the comparatively lesser internal tasks as compared to the function used in the above method.
Python3
# Python3 code to demonstrate working of
# Removing Initial word from string
# Using partition()
# initializing string
test_str = "
GeeksforGeeks is best & quot
# printing original string
print(& quot
The original string is : & quot
+ test_str)
# Using partition()
# Removing Initial word from string
res = test_str.partition(' ')[2]
# printing result
print(& quot
The string after omitting first word is : & quot
+ str(res))
Output : The original string is : GeeksforGeeks is best
The string after omitting first word is : is best
Method #3: Using join() and split() methods
Python3
# Python3 code to demonstrate working of
# Removing Initial word from string
# Using split() and join()
# initializing string
test_str = "GeeksforGeeks is best"
# printing original string
print("The original string is : " + test_str)
# Using split() and join()
# Removing Initial word from string
x = test_str.split()
res=" ".join(x[1:])
# printing result
print("The string after omitting first word is : " + str(res))
OutputThe original string is : GeeksforGeeks is best
The string after omitting first word is : is best
Method #4: Using pop() function
Python3
# Python3 code to demonstrate working of
# Removing Initial word from string
# initializing string
test_str = "GeeksforGeeks is best"
# printing original string
print("The original string is : " + test_str)
x = test_str.split()
x.pop(0)
res = ' '.join(x)
# printing result
print("The string after omitting first word is : " + str(res))
OutputThe original string is : GeeksforGeeks is best
The string after omitting first word is : is best
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #5: Using index() function
Python3
# Python3 code to demonstrate working of
# Removing Initial word from string
# Using slicing
# initializing string
test_str = "GeeksforGeeks is best"
# printing original string
print("The original string is : " + test_str)
# Removing Initial word from string
# Using slicing
res = test_str[test_str.index(" ")+1:]
# printing result
print("The string after omitting first word is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original string is : GeeksforGeeks is best
The string after omitting first word is : is best
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1), as we are just using a few variables for storing intermediate results.
Method 6 : using the find() method and string slicing.
- Initialize the string.
- Use the find() method to find the index of the first space character.
- Use string slicing to extract the substring after the first space character.
- Print the resulting string.
Python3
# Python3 code to demonstrate working of
# Removing Initial word from string
# Using find() method
# initializing string
test_str = "GeeksforGeeks is best"
# printing original string
print("The original string is : " + test_str)
# Removing Initial word from string
# Using find() method
index = test_str.find(' ')
res = test_str[index+1:]
# printing result
print("The string after omitting first word is : " + str(res))
OutputThe original string is : GeeksforGeeks is best
The string after omitting first word is : is best
Time complexity: O(n) - where n is the length of the string.
Auxiliary space: O(1) - constant extra space is used.
Similar Reads
Python - Remove substring list from String Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by
3 min read
Python - Remove suffix from string list To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix. This involves checking each string in the list and ensuring it doesn't have the unwanted suffix at the end, resulting in a list with only the desired elements.Using list comprehensionUsing
3 min read
Python - Remove Punctuation from String In this article, we will explore various methods to Remove Punctuations from a string.Using str.translate() with str.maketrans()str.translate() method combined with is str.maketrans() one of the fastest ways to remove punctuation from a string because it works directly with string translation tables
2 min read
Python | Remove prefix strings from list Sometimes, while working with data, we can have a problem in which we need to filter the strings list in such a way that strings starting with a specific prefix are removed. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + remove() + startswith() The combinati
5 min read
Python - Remove after substring in String Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring includin
3 min read
Remove spaces from a string in Python Removing spaces from a string is a common task in Python that can be solved in multiple ways. For example, if we have a string like " g f g ", we might want the output to be "gfg" by removing all the spaces. Let's look at different methods to do so:Using replace() methodTo remove all spaces from a s
2 min read
Removing newline character from string in Python When working with text data, newline characters (\n) are often encountered especially when reading from files or handling multi-line strings. These characters can interfere with data processing and formatting. In this article, we will explore different methods to remove newline characters from strin
2 min read
How to Remove a Substring in Python? In Python, removing a substring from a string can be achieved through various methods such as using replace() function, slicing, or regular expressions. Depending on your specific use case, you may want to remove all instances of a substring or just the first occurrence. Letâs explore different ways
2 min read
Python | Remove the given substring from end of string Sometimes we need to manipulate our string to remove extra information from the string for better understanding and faster processing. Given a task in which the substring needs to be removed from the end of the string using Python. Â Â Remove the substring from the end of the string using Slicing In
3 min read
Remove Multiple Characters from a String in Python Removing multiple characters from a string in Python can be achieved using various methods, such as str.replace(), regular expressions, or list comprehensions. Each method serves a specific use case, and the choice depends on your requirements. Letâs explore the different ways to achieve this in det
2 min read