Bisexual Pride

No Starch Press published the third edition of Python Crash Course by Eric Matthes last week. I’d read the second edition before, but two chapters into the third edition and I’ve already learned a bunch of useful new methods! Let’s learn how to remove prefixes and suffixes from strings in Python.

The old way

In Python versions ≤ 3.8, the easiest way to remove a prefix (or suffix) from a string is to check if the string starts with the prefix (or ends with the suffix) and slice it.

# Phoebe Bridgers is an American singer, songwriter, and record producer.
# She has the best domain name ever.
url = "https://phoebefuckingbridgers.com"

prefix = "https://"

if url.startswith(prefix):
url = url[len(prefix):]

print(url) # "phoebefuckingbridgers.com"

suffix = ".com"

if url.endswith(suffix):
url = url[:-len(suffix)]

print(url) # "phoebefuckingbridgers"

In this example:

  • The value of the url variable is the string "https://phoebefuckingbridgers.com".
  • The value of the prefix variable is the string "https://".
  • The value of the suffix variable is the string ".com".

To remove the prefix, I check if the value of the url variable starts with the value of the prefix variable. If so, I get the length of the prefix variable’s value. I use this as the start value for the slice, i.e. the index of the first character that should be included in the new string. I create a slice of the url variable’s value starting from this index. Because I don’t specify a stop value, the slice continues until the end of the string. Finally, I reassign the url variable with the new value.

To remove the suffix, I check if the value of the url variable ends with the value of the suffix variable. If so, I get the length of the suffix variable’s value. I use this as the stop value for the slice, i.e. the index of the first character that should not be included in the new string. Because I negate the value, it counts backwards from the end of the string. I create a slice of the url variable’s value that stops before this index. Because I don’t specify a start value, the slice starts from the beginning of the string. Finally, I reassign the url variable with the new value.

The new way

In Chapter 2 of Python Crash Course, subsection Removing Prefixes, Matthes introduces two new methods that simplify this code. In Python versions ≥ 3.9, you can use the str.removeprefix() and str.removesuffix() methods.

url = "https://phoebefuckingbridgers.com"

prefix = "https://"
url = url.removeprefix(prefix)

print(url) # "phoebefuckingbridgers.com"

suffix = ".com"
url = url.removesuffix(suffix)

print(url) # "phoebefuckingbridgers"

Not only is this more readable, but if the string doesn’t start with the prefix (or end with the suffix), these methods return a copy of the original string. This removes the need for a conditional statement.

Recap

  • In Python versions ≤ 3.8, the easiest way to remove a prefix (or suffix) from a string is to check if the string starts with the prefix (or ends with the suffix) and slice it.
  • In Python versions ≥ 3.9, you can use the str.removeprefix() and str.removesuffix() methods.

For more like this, I recommend buying a copy of Python Crash Course. It’s a wonderful book with a project-based method of teaching. Having a real context is a much better way of learning than writing meaningless foobar code.