String methods

Methods return new strings, because strings can't change. A common bug is calling s.strip() and not using the result.

  • split() with no argument splits on any run of whitespace and drops empty pieces. split(",") splits on each comma exactly.
  • sep.join(parts) is the reverse. It is called on the separator.
  • strip, lower, upper, replace, startswith, endswith.
  • isdigit, isalpha and isalnum test every character.

find returns -1 when the text is missing, while index raises ValueError. A bug to watch for: if s.find("x"): is false when the match is at position 0, and true when it is missing.

Run it, then change "a,,b" to see how split(",") keeps empty pieces.