Get the latest tech news

Python Gotcha: strip, lstrip, rstrip can remove more than expected


The Python strip, lstrip, and rstrip functions can have unexpected behavior. Even though this is documented, non-default values passed to these functions can lead to unexpected results and how Python 3.9 solved this with two new functions.

In this case it is all letters in my last name, plus the space character: wegner Lower case all letters in the input string, resulting in andrew wegner From the right hand side of the string, begin removing characters that are in the input list. In this case that means that rengew wer are removed (right to left) and then the d in andrew is encountered so that rstrip function stops. In the PEP, it explicitly calls out the confusion users have about the*strip() functions and how they behave.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Strip

Strip

Photo of Python Gotcha

Python Gotcha

Photo of rstrip

rstrip

Related news:

News photo

The Best Govee Smart Lights (2023): M1 Light Strip, Envisual T2, and More Tips