Removing the Initial Word from a String Using Python


Python is a commonly used programming language used for many different purposes such as Web development, data science, machine learning and to perform many different processes with automation. In this article we will learn how to remove the initial word from a string using python.

Different Methods to Remove Initial Word from a String

Split Function

In this method we will split the strings into substring to remove the initial string. We will use the split function for the same purpose. The syntax to be used for removing the initial word from a string using split function is as follows:

def initial_string(string):   #The string is given as input
    data = string.split()     #The string is then split
    if len(data) > 1:         #If it has more than one element then it is joined
        return ' '.join(data[1:])
    else:
        return ""

Example

Let’s take an example to understand its use:

def initial_string(string):   #The string is given as input
    data = string.split()     #The string is then split
    if len(data) > 1:         #If it has more than one element then it is joined
        return ' '.join(data[1:])
    else:
        return ""
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string)

Output

The output of the above example will be as follows:

We will work on Python.  

re module

The re module provides regular expression support. It uses a specific pattern to find a specific string or sub string. We will use the same for finding the initial string and then remove it. The syntax to remove the initial word from a string using re module is as follows:

import re  # Do not forget to import re module or else error might occur.

def initial_string(string):  #The string is given as input
    pattern = r'^\w+\s+'  # A pattern is defined to find the initial string
    return re.sub(pattern, '', string)   # The initial string is substituted with an empty space

Example

The example of the above method is as follows:

import re  # Do not forget to import re module or else error might occur.

def initial_string(string):  #The string is given as input
    pattern = r'^\w+\s+'  # A pattern is defined to find the initial string
    return re.sub(pattern, '', string)   # The initial string is substituted with an empty space
whole_string = "Today We will work on Python."
new_string = initial_string(whole_string)
print(new_string)  #Displaying the output after removing the initial string

Output

The output of the above method will be as follows:

We will work on Python. 

Find Function

The find function is used to find a specific word or element in a string using its position. In this method we will use the same function to find the word at first position and remove it. The syntax to remove the initial word using the find function is as follows:

def initial_string(string):  #The string is given as input
    first_word = string.find(' ')   #Find() is used to find the initial word of the string and if space is found it returns back
    if first_word != -1:
        return string[first_word + 1:]
    else:
        return ""

Example

The example using the above syntax is as follows:

def initial_string(string):  #The string is given as input
    first_word = string.find(' ')   #Find() is used to find the initial word of the string and if space is found it returns back
    if first_word != -1:
        return string[first_word + 1:]
    else:
        return ""
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string)  #Displaying the output after removing the initial string

Output

The output of the above code will be as follows:

We will work on Python. 

Space Delimiter

We split the string into two different parts using the string split function and then the string with more than element is removed and the initial word is removed. The syntax to remove the initial word from the string using space delimiter is as follows:

def initial_string(string):  #The string is given as input
    first = string.split(' ', 1)  #Second argument to split the string into two parts   
    if len(first) > 1:    # Checking for more than 1 element
        return first[1]   #If more than 1 element is present it returns the first part
    else:
        return ""       #If only 1 element is present it returns the second part

Example

The example of the above method will be as follows:

def initial_string(string):  #The string is given as input
    first = string.split(' ', 1)  #Second argument to split the string into two parts   
    if len(first) > 1:    # Checking for more than 1 element
        return first[1]   #If more than 1 element is present it returns the first part
    else:
        return ""       #If only 1 element is present it returns the second part
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string)   #Displaying the output after removing the initial string

Output

The output of the above example will be as follows:

We will work on Python.  

Partition Method

The Python partition function divides a string into three pieces based on the chosen separator. As a result, the separator itself, the region before the separator, and the region after the separator are combined to form a tuple. The syntax to remove the initial word from the string using partition method is as follows:

def initial_string(string):   #The string is given as input
    _, separator, remainder = string.partition(' ') # The partition method is used to seprate the string at the first space in the string
    if separator:           #If a seprator will be found then it will return remainder or else a empty space will return
        return remainder
    else:
        return ""

Example

The example using the above method is as follows:

def initial_string(string):   #The string is given as input
    _, separator, remainder = string.partition(' ') # The partition method is used to seprate the string at the first space in the string
    if separator:           #If a seprator will be found then it will return remainder or else a empty space will return
        return remainder
    else:
        return ""
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string)   #Displaying the output after removing the initial string

Output

The output of the above code will be as follows:

We will work on Python.  

Conclusion

There are many different methods to remove the initial word from the string using python and it is necessary for a beginner to have knowledge about all these methods of programming so that he/she can use the suitable logic in suitable problems while working on different projects in different fields. A programmer can use the method of his/her choice and whichever suits the field of programming and whichever reduces the programming time.

Updated on: 01-Aug-2023

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements