Skip to content

Latest commit

 

History

History
21 lines (15 loc) · 658 Bytes

trim-strings-in-python.md

File metadata and controls

21 lines (15 loc) · 658 Bytes

Trim strings in Python

In Python strings can be trimmed using the following three functions:

  • strip(): returns a new string after removing any leading and trailing whitespaces including tabs (\t).
  • rstrip(): returns a new string with trailing whitespace removed. It’s easier to remember as removing white spaces from “right” side of the string.
  • lstrip(): returns a new string with leading whitespace removed, or removing whitespaces from the “left” side of the string.

Equivalent of C#'s Trim() funciton is strip().

str = '  abc  '
>>> str.strip()
'abc'
>>> str.rstrip()
'  abc'
>>> str.lstrip()
'abc  '