Python program to delete prefix substring from the given string


In this article, we will learn how to create a Python program to delete prefix substrings from a given string. Sometimes in data, we have a prefix of multiple words of the same type and if we want to delete them then it will take a lot of time but this can be solved by giving an algorithm to it. A prefix is a group of characters that are added to the beginning.

Let’s take an example to understand this.

The given string variable is “My shirt color is Red” and delete the prefix variable i.e. “My”.

Then, the final output becomes “shirt color is Red”.

Syntax

startswith()

This is the predefined method used in Python and if the string starts with the given value it returns true, otherwise false.

lstrip()

This is the predefined method used in Python and removes the given prefix character from the beginning of the string.

removeprefix()

This is an in-built function that returns true if the string matches to beginning of characters.

Example 1

In this program, we are initializing the two variables- ‘str_name’ and ‘p_fix’ which will store the value of the input string and prefix substring respectively. Then we are using if-statement and do the following −

  • if str_name.startswith( p_fix ) − Through this representation, it will check whether the value of the prefix substring is valid or not. If it is valid then it returns true otherwise false.

  • str_name = str_name[ len(p_fix): ] − Through this representation, the given length is sliced and deletes the prefix substring.

Finally, we are printing the result with the help of a variable named ‘str_name’.

str_name = "Red pen"
p_fix = "Red"
if str_name.startswith( p_fix ):
   str_name = str_name[ len(p_fix): ]
print( "After deleting the given prefix:", str_name )

Output

After deleting the given prefix:  pen

Example 2

In this program, we will initialize the two variables- ‘str_name’ and ‘prefix_name’ which will store the value of the input string and prefix substring respectively. Then use the built-in function to set the variable named ‘prefix_name’ into it. This will remove the beginning of the substring and define the term prefix. Finally, we are printing the result with the help of the variable ‘str_name’.

str_name = "The sky is blue in color"
prefix_name = "The"
str_name = str_name.lstrip( prefix_name )
print( "After deleting the given prefix:", str_name )

Output

After deleting the given prefix:  sky is blue in color

Example 3

In the following example, we will start the program by storing the input string in the variable s_prefix. Then use the built-in method named removeprefix() that accepts the substring to verify the specific value and returns true to get the result of rest of the substring.

s_prefix = 'qwertyuiop'
print(s_prefix.removeprefix('qwe'))

Output

rtyuiop

Example 4

In the following program, we will use the function with a def keyword named removeprefix() that accepts two parameters- org_str(given string) and prefix(store the value of the variable rest_substring). Then use the if-else statement to check the condition of the variable prefix that returns either length of the originally given string or else returns the given string. Next, set the values of two variables- org_str and rest_substring. After processing all these steps it will print the result with the help of variables- org_str and rest_substring.

def removeprefix(org_str, prefix):
   if org_str.startswith(prefix):
      return org_str[len(prefix):]
   else:
      return org_str
org_str = 'abcdefghijklmnopqrstuvwxyz'
rest_substring = removeprefix(org_str, 'abcdefghijkl')
print('The original string: ', org_str)
print('After removing the prefix: ', rest_substring)

Output

The original string:  abcdefghijklmnopqrstuvwxyz
After removing the prefix:  mnopqrstuvwxyz

Conclusion

We explored the concept to delete the prefix substring from the given string. Then we saw there are four examples of this program and solving the ways are different by taking the length operation and the predefined method. This type of program is normally used to delete multiple words starting with the same prefix.

Updated on: 01-Jun-2023

144 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements