0

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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    Please edit your question to show the exact code that is producing the incorrect output including the spaces that should have been stripped. The code in your question won't produce any output because it has a typo, so it can't be run. See [mcve]. – kaya3 Feb 12 '22 at 01:08

1 Answers1

0

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 .
Udesh
  • 2,415
  • 2
  • 22
  • 32