Real Python: Strip Characters From a Python String
By default, Python’s.strip()method removes whitespace characters from both ends of a string. To remove different characters, you can pass a string as an argument that specifies a set of characters to remove. The.strip()method is useful for tasks like cleaning user input, standardizing filenames, and preparing data for storage.
By the end of this video course, you’ll understand that:
- The
.strip()methodremoves leading and trailing whitespacebut doesn’t remove whitespace from the middle of a string. - You can use
.strip()to removespecified charactersfrom both ends of the string by providing these characters as anargument. - With the related methods
.lstrip()and.rstrip(), you can remove characters fromone sideof the string only. - All three methods,
.strip(),.lstrip(), and.rstrip(),remove character sets, not sequences. - You can use
.removeprefix()and.removesuffix()tostrip character sequencesfrom the start or end of a string.
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days.>> Click here to learn more and see examples]
https://realpython.com/courses/strip-characters-from-string/