Open In App

Python | Removing Initial word from string

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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))


Output

The 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))


Output

The 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


Output

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

  1. Initialize the string.
  2. Use the find() method to find the index of the first space character.
  3. Use string slicing to extract the substring after the first space character.
  4. 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))


Output

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads