Python’s strip() returns a new string with whitespace removed from both ends. It leaves internal whitespace unchanged; use lstrip() or rstrip() when only one side should change. section 6.4.3
Use the optional character argument carefully:
- strip(chars) removes any characters listed in chars from either end, repeatedly, rather than removing one exact substring. section 6.4.3
- Use rstrip() to remove trailing newlines from lines read from files; use removeprefix() or removesuffix() for exact matches. section 6.4.3
- In pandas, apply .str.strip("%") to a column before converting the cleaned strings to numbers. section 24.3.2
Whitespace and character sets
x = " Hello, World\t\t " x.strip() # 'Hello, World' x.lstrip() # 'Hello, World\t\t ' x.rstrip() # ' Hello, World' x = "www.python.org" x.strip(".gorw") # 'python' x.removeprefix("www.") # 'python.org'
temp[17] = pd.to_numeric(temp[17].str.strip("%")).div(100)
Chain column cleanup, numeric conversion, and division when transforming percentage strings into fractions. section 24.3.2



