[Tutor] Trouble Removing Whitespace

Peter Otten __peter__ at web.de
Mon Nov 7 04:14:15 EST 2016


Jason Durnen wrote:

> Hello,
> I am totally new to programming but thought I would give it a shot. I
> recently purchased the Python Crash Course book and have run in to a
> problem in the second chapter. The section that talks about removing
> whitespace in code to be more precise.
> 
> This is the code that I type in, but the result will not trim the extra
> space. The first group is how it is shown in the book, but when it prints,
> the  '  is not included anywhere.
> 
> favorite_language = 'python '
> print (favorite_language)
> favorite_language = favorite_language.rstrip()
> print(favorite_language)

I don't know what you use for your experiments, for my demonstration below I 
use Python 3's "interactive interpreter" which is invoked from the command 
line with

$ python3

The $ is the prompt which you don't have to type yourself and which will 
look differently on Windows. If all goes well you'll see something similar 
to

Python 3.4.3 (default, Sep 14 2016, 12:36:27) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Let's start our experimentation:

>>> print("python   ")
python   
>>> print("python")
python

OK, the two strings are different, but you do not see the difference because 
it consists of whitespace. One way around this is to print something else 
afterwards:

>>> print("python   ", 3)
python    3
>>> print("python", 3)
python 3

Now you should see a difference, more space between "python" and "3" in the 
first case. Invoking rstrip() removes that extra space at the end of the 
first string:

>>> print("python   ".strip(), 3)
python 3

For debugging purposes you can convert a string using repr() which will not 
only add quotes, but also change newline and a few other characters to 
escape sequences. You can invoke repr() manually

>>> print(repr("python   "))
'python   '
>>> print(repr("python   ".strip()))
'python'

or implicitly by typing an expression in the interactive interpreter:

>>> "python   "
'python   '
>>> "python   ".strip()
'python'

Here's a triple-quoted string spanning over multiple lines to demonstrate 
newline handling:

>>> """triple
... quoted
... string
... """
'triple\nquoted\nstring\n'

> In this section, I added double quotations around the single quotation and
> the word python, however I can't get it to remove the whitespace when it
> print the word. Is there something that I'm missing?

rstrip() removes whitespace from the end of the string, but at the end of 
your string is now a "'", not a " ". Therefore nothing is removed.

> 
> favorite_language = "'Python '"
> print(favorite_language)
> favorite_language = favorite_language.rstrip()
> print(favorite_language)
> 
> The following is the output I get from the two examples I have above:
> python
> python
> 'Python '
> 'Python '
> Press any key to continue . . .
> 
> Any help you could give me would be greatly appreciated!
> Thanks for your time.
> 
> Jason Durnen
> jasondurnen at gmail.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list