Stripping whitespace in python

295 views Asked by At

I'm reading the python crash course book. The author talks about stripping whitespace, and how to eliminate them in strings:

He gives an example like this:

favorite_language='python '
print(favorite_language.rstrip())

-- 'python'

You can see the the whitespace is gone. Whenever I try it in sublime it gives me the first string ('python ') with extra whitespace.

1

There are 1 answers

0
devpa On

rstrip(): returns a new string with trailing whitespace removed. It’s easier to remember as removing white spaces from “right” side of the string.

please visit this site regarding formatting string in python.

#!/usr/bin/env python3.10
favorite_language=' python language '

print("."+favorite_language.rstrip()+".")#removes trailing white spaces
print("."+favorite_language.strip()+".")#removes white spaces from both leading and trailing
print("."+favorite_language.lstrip()+".")#removes ending white spaces

output:

$ python3.10 myClass.py 
. python language.
.python language.
.python language .