How To Remove The Prefix Of A String In Python?

Summary: This blog explains the various ways one can remove the prefix of a string in Python. How To Remove The Prefix Of A String In Python?

Python 3.9 provides the removeprefix() method to aid string manipulation. The removeprefix() method of the Python String Object removes prefixes from any string. One needs to supply the prefix as an argument to the removeprefix() method. For example, my_string.removeprefix('xxx') removes the prefix 'xxx' from my_string.

>>> 'xxxhello world'.removeprefix('xxx')
'hello world'

Note: All the solutions provided below have been verified using Python 3.9.0b5

Problem Formulation

Imagine that one has the following string in Python.

my_string = "PKG_CONFIG_PATH=/usr/local/opt/sqlite/lib/pkgconfig"

How does one get rid of the prefix “PKG_CONFIG_PATH=” and get the remaining string?

new_string = "/usr/local/opt/sqlite/lib/pkgconfig"

Background

In fields such as IT, one often finds the need to remove the prefix of a string. Removal of string prefixes is also needed when scrubbing data after its extraction. Luckily Python has a whole bunch of robust string processing capabilities to do this.This blog article explores different ways, using Python, to remove the prefix of a string.

The Simple Method with removeprefix()

Python 3.9 provides the removeprefix() method to aid string manipulation. The removeprefix() method of the Python String Object removes prefixes from any string. One needs to supply the prefix as an argument to the removeprefix() method. The following steps explain how this is done.

$ python
Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) 
>>>
>>> ## This is the original string whose prefix needs to be removed. 
>>> my_string = "PKG_CONFIG_PATH=/usr/local/opt/sqlite/lib/pkgconfig"
>>> 
>>> ## Provide the prefix as an argument to removeprefix. 
>>> new_string = my_string.removeprefix("PKG_CONFIG_PATH=")
>>> 
>>> new_string
'/usr/local/opt/sqlite/lib/pkgconfig'
>>> 

Note: The removeprefix() method is only available in Python 3.9 and later. Please refer to the Official Python Documentation for more details. Also note that one could do all of this in one line. Consider the following…

$ python
Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) 
>>>
>>> ## Removing the prefix as a one liner. 
>>> new_string = "PKG_CONFIG_PATH=/usr/local/opt/sqlite/lib/pkgconfig".removeprefix("PKG_CONFIG_PATH=")
>>> 
>>> new_string
'/usr/local/opt/sqlite/lib/pkgconfig'

For the curious readers, the removesuffix() method removes suffixes from any string.

$ python
Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) 
>>>
>>> ## This is the original string whose suffix (“.org”) needs to be removed. 
>>> my_string = "howToRemoveThisSuffix.org"
>>> 
>>> ## Provide the suffix as an argument to removesuffix. 
>>> new_string = my_string.removesuffix(".org")
>>> 
>>> new_string
'howToRemoveThisSuffix'
>>> 

Note: The removesuffix() method is only available in Python 3.9 and later. Please refer to the Official Python Documentation for more details.

But, What If I Do Not Have Python 3.9?

Never fear when Python’s here!!  Python is a very flexible scripting language. There is something for everyone!! There are many ways to slice the same apple, so to speak. The following example shows another method to remove the prefix. This method uses the len() method along with String Slicing to remove the prefix.

>>> ## This is the original string whose prefix needs to be removed. 
>>> my_string = "PKG_CONFIG_PATH=/usr/local/opt/sqlite/lib/pkgconfig"
>>>
>>> ## This is the prefix that needs to be removed. 
>>> my_prefix = “PKG_CONFIG_PATH=”
>>>
>>> ## Use the len() method to remove the prefix. Note that the len() method returns the
>>> ## length of the provided string argument (i.e. my_prefix). String slicing is then used
>>> ## used to select and return the remaining string.     
>>> new_string = my_string[len(my_prefix):]
>>> 
>>> new_string
'/usr/local/opt/sqlite/lib/pkgconfig'
>>> 

len() in the above method, returns the length of the string. String Slicing (i.e. string[a:b]) returns the rest of the string. Note that omitting 'b' means, select from position 'a' to end of the string. Again, here is the one-liner example.

>>> ## Removing the prefix as a one liner. 
>>> new_string = "PKG_CONFIG_PATH=/usr/local/opt/sqlite/lib/pkgconfig"[len("PKG_CONFIG_PATH="):]
>>> new_string
'/usr/local/opt/sqlite/lib/pkgconfig'
>>> 

A curious reader might ask, “Well how does one remove the suffix without using removesuffix()?” The answer is similar to above, except notice the subtle change in the slicing notation.

$ python
Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) 
>>>
>>> ## This is the original string whose suffix (“.org”) needs to be removed. 
>>> my_string = "howToRemoveThisSuffix.org"
>>> 
>>> ## This is the suffix that needs to be removed. 
>>> my_suffix = ".org"
>>>
>>> ## Use the len() method to remove the suffix. Note that the len() method returns the
>>> ## length of the provided string argument (i.e. my_suffix). String slicing is then used
>>> ## used to select and return the remaining string.     
>>> new_string = my_string[:-len(my_suffix)]
>>> 
>>> new_string
'howToRemoveThisSuffix'
>>> 

String Slicing (string[a:b]) is used again. However in contrast to above, ‘a’ is omitted. This tells Python to start from the beginning of the string. ‘b’ is a negative number. This tells Python to count len(my_suffix) characters from the end of my_string. Yes, the counting is done backwards from the end of the string. This drops the suffix when forming the new_string.

Are There Other Ways To Remove A Prefix?

Yes, there are other ways… The Python String Object has a method called partition(). If one knows the separator then use the partition() method to remove the prefix. The partition() method will separate a string at the first occurrence of the separator. This method will return a 3-tuple that contains the following…

  • The part before the separator
  • The separator itself
  • The part after the separator.

The method returns the original string if the separator is not found. In this case the original string is the first element of the returned 3-tuple. The other elements are empty strings.

$ python
Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) 
>>>
>>> ## This is the original string whose prefix needs to be removed. 
>>> my_string = "PKG_CONFIG_PATH=/usr/local/opt/sqlite/lib/pkgconfig"
>>>
>>> ## The partition() method splits the original string at the separator “=”. In this
>>> ## example, this separator is the first and only occurrence.
>>> new_string = my_string.partition("=")
>>> ## new_string is a 3-Tuple
>>> new_string
('PKG_CONFIG_PATH', '=', '/usr/local/opt/sqlite/lib/pkgconfig')
>>>
>>> ## The third element is the string with the prefix removed. 
>>> new_string[2]
'/usr/local/opt/sqlite/lib/pkgconfig'

But how does one remove the suffix? Use the rpartition() Python String object method to remove the suffix. The rpartition() method separates a string at the last occurrence of the separator. Similar to partition(), the rpartition() method will return a 3-tuple that contains the following…

  • The part before the separator
  • The separator itself
  • The part after the separator.

The method returns the original string if the separator is not found. In this case the original string is the last element of the returned 3-tuple. The other elements are empty strings.  Consider the following example…

$ python
Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) 
>>>
>>> ## This is the original string whose suffix (“.org”) needs to be removed. 
>>> my_string = "howToRemoveThisSuffix.org"
>>> 
>>> ## The rpartition() method splits the original string at the separator “.”. In this
>>> ## example, this separator is the last and only occurrence.
>>> new_string = my_string.rpartition(".")
>>>
>>> ## Again, new_string is a 3-Tuple
>>> new_string
('howToRemoveThisSuffix', '.', 'org')
>>> 
>>> ## In this example, the first element is the string with the suffix removed. 
>>> new_string[0]
'howToRemoveThisSuffix'

What About The lstrip() and rstrip() Methods?

Ah!! Good question!!!.  One has to be careful about using the lstrip() and rstrip() String Object methods. The lstrip() method takes a character set as an argument. This set is not a prefix; lstrip() strips all combinations of its character set argument. lstrip() uses its argument to remove leading characters from the original string, if it can. This is better understood with an example.

$ python
Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) 
>>>
>>> ## This is the original string whose prefix needs to be removed.
>>> ## The prefix “abcd=” should be removed.
>>> my_string = "abcd=abadcoffee"
>>>
>>> ## So “abcd=” is provided as the argument set to lstrip()
>>> new_string = my_string.lstrip("abcd=")
>>>
>>> ## Bad idea!! lstrip() will keep stripping all leading characters until it encounters
>>> ## a character that is not in its set, i.e. “o”
>>> new_string
'offee'
>>> ## “abadcoffee” was expected. Instead the result was “offee”. Remember
>>> ## lstrip() strips *all* combinations of its character set argument.

The rstrip() method works in a similar fashion.  Except, it uses its argument to remove trailing characters from the original string, if it can. As with lstrip() this set is not a suffix; rstrip() strips all combinations of its character set argument. This too is better understood with an example.

$ python
Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) 
>>>
>>> ## This is the original string whose suffix needs to be removed.
>>> ## The suffix “.abcd” should be removed.
>>> my_string = "deadbeefabad.abcd"
>>>
>>> ## So “.abcd” is provided as the argument set to rstrip()
>>> new_string = my_string.rstrip(".abcd")
>>>
>>> ## Bad idea!! rstrip() will keep stripping all trailing characters until it encounters
>>> ## a character that is not in its set, i.e. “f”
>>> new_string
'deadbeef'
>>> ## “deadbeefabad” was expected. Instead the result was “deadbeef”. Remember
>>> ## rstrip() strips *all* combinations of its character set argument

Finxter Academy

This blog was brought to you by Girish, a student of Finxter Academy. You can find his Upwork profile here.

Reference

All research for this blog article was done using Python Documents, the Google Search Engine and the shared knowledge-base of the Finxter Academy and the Stack Overflow Communities.