Python str.strip() Function Removes Leading/Trailing Characters

By default, str.strip in #Python removes leading/trailing whitespace: s = ' a b c ' s.strip() # returns 'a b c' It takes an optional argument, naming leading/trailing characters to remove. Great for cleaning strings: s = '"Hello!"' s.strip(string.punctuation) # 'Hello'

  • No alternative text description for this image

Haiku for you, thank you ChatGPT: str.strip() whispers, Shaving space from either edge, Punctuation yields.

The argument is a list of all *characters* to be removed and not the “word” listed, resulting in more being stripped than expected.

See more comments

To view or add a comment, sign in

Explore content categories