May 2019
Beginner
528 pages
29h 51m
English
There are several string methods for removing whitespace from the ends of a string. Each returns a new string leaving the original unmodified. Strings are immutable, so each method that appears to modify a string returns a new one.
Let’s use string method strip to remove the leading and trailing whitespace from a string:
In [1]: sentence = '\t \n This is a test string. \t\t \n'In [2]: sentence.strip()Out[2]: 'This is a test string.'
Method lstrip removes only leading whitespace:
In [3]: sentence.lstrip()Out[3]: 'This is a test string. \t\t \n'
Method rstrip removes only trailing whitespace:
In ...Read now
Unlock full access