Sitemap

EASY Python Mistake!!

Removing Substrings Correctly

--

Press enter or click to view image in full size

When working with strings in Python, it’s easy to overlook the subtleties of certain methods — especially when removing specific characters or prefixes. Here’s a quick but essential guide on how to avoid a common pitfall when trying to clean up URLs by removing prefixes like "www.".

Let’s say you have a list of domain names, and you want to remove the "www." at the beginning of each one. This sounds straightforward, but one small misstep in choosing the method can lead to unexpected results.

The Wrong Way: Using lstrip()

Example Code

Imagine your code looks something like this:

links = ["www.google.com", "www.wikipedia.org", "openai.com"]

for link in links:
print(link.lstrip("www."))

What’s Happening Here?

You might expect this code to remove only "www." from each URL. However, lstrip() doesn’t work this way. The lstrip() method takes a list of characters to remove, not an exact substring. So when you pass "www." to lstrip(), Python interprets it as a list of individual characters: [w, w, w, .].

Get Jackson Davis’s stories in your inbox

Join Medium for free to get updates from this writer.

This means lstrip() will strip all occurrences of “w” and “.” from the beginning of the string, not just the "www." prefix.

The Result

In our example, using lstrip("www.") on "www.wikipedia.org" results in "ikipedia.org" instead of the expected "wikipedia.org". The leading "W" is removed because lstrip() is acting on each character individually, not on the substring "www.".

The Right Way: Using removeprefix()

The removeprefix() method, introduced in Python 3.9, is specifically designed to remove a substring if it appears at the start of a string. This method treats the entire input string as a prefix to be removed, solving the problem we encountered with lstrip().

Corrected Code

Here’s how to correctly use removeprefix():

links = ["www.google.com", "www.wikipedia.org", "openai.com"]

for link in links:
print(link.removeprefix("www."))

Why It Works

The removeprefix("www.") method looks for exactly "www." at the beginning of each string. If it finds this substring, it removes it; if not, it leaves the string unchanged. This way, "www.wikipedia.org" correctly becomes "wikipedia.org", without mistakenly removing the "W".

Output

Running the corrected code will give the expected results:

google.com
wikipedia.org
openai.com

Why This Matters

Understanding the difference between lstrip() and removeprefix() is essential for accurate string manipulation in Python. Using removeprefix() ensures that you don’t unintentionally remove characters from your strings and helps avoid subtle bugs, especially in applications involving text parsing and cleanup.

If you found this tip helpful, be sure to share it with others who might benefit from a closer look at Python’s string methods!

--

--

Jackson Davis
Jackson Davis

Written by Jackson Davis

Game dev, AI, Python, coding enthusiast.