Re: [Tutor] String method strip() not working

2015-03-07 Thread Dave Angel

On 03/07/2015 08:15 AM, Akash Shekhar wrote:

I am trying to learn how to use strip() method. It is supposed to cut out
all the whitespace as I read in the tutorial. But the code is not working.

Here's my code:

sentence = Hello, how are you?




print(sentence)




print(sentence.strip())




input(\n\nPress enter key to exit.)





Here's it's output:

Hello, how are you?

Hello, how are you?

Press enter key to exit.




Both results are same.

P.S.: I am using Python 3.1 IDLE on Windows 7.


Thanks for mentioning the python version and OS.

You don't have any whitespace at the beginning nor end of the string 
bound to sentence.  So there's nothing to strip.  By the way, if you're 
checking such a function, it's sometimes more informative to write

   print(repr(sentence))
which will add quotes at begin and end, and show newlines and tabs as 
escape sequences.


If you only want to strip from one end of the string, you'd use lstrip() 
or rstrip().


If you're also trying to remove characters from the middle of the 
string, you might use translate() or the string method replace().  For 
example, to remove all spaces from a string, use

sentence.replace( , )

--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String method strip() not working

2015-03-07 Thread Alan Gauld

On 07/03/15 15:17, Mark Lawrence wrote:


 S.strip([chars]) - string or unicode

 Return a copy of the string S with leading and trailing
 whitespace removed.
 If chars is given and not None, remove characters in chars instead.
 If chars is unicode, S will be converted to unicode before stripping


Presumably Python 2 (or earlier Python 3) help output.  In Python 3
strings are always unicode, so that last line has gone.


Good catch, especially since the OP specifically said v3.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String method strip() not working

2015-03-07 Thread Alan Gauld

On 07/03/15 13:15, Akash Shekhar wrote:

I am trying to learn how to use strip() method. It is supposed to cut out
all the whitespace as I read in the tutorial.


Read it again more closely.

---
Help on built-in function strip:

strip(...)
S.strip([chars]) - string or unicode

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping
--

So it only strips leading and training whitespace, not the whitespace 
inside the string. (Use str.translate() for that)

HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String method strip() not working

2015-03-07 Thread Mark Lawrence

On 07/03/2015 14:54, Alan Gauld wrote:

On 07/03/15 13:15, Akash Shekhar wrote:

I am trying to learn how to use strip() method. It is supposed to cut out
all the whitespace as I read in the tutorial.


Read it again more closely.

---
Help on built-in function strip:

strip(...)
 S.strip([chars]) - string or unicode

 Return a copy of the string S with leading and trailing
 whitespace removed.
 If chars is given and not None, remove characters in chars instead.
 If chars is unicode, S will be converted to unicode before stripping


Presumably Python 2 (or earlier Python 3) help output.  In Python 3 
strings are always unicode, so that last line has gone.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] String method strip() not working

2015-03-07 Thread Akash Shekhar
I am trying to learn how to use strip() method. It is supposed to cut out
all the whitespace as I read in the tutorial. But the code is not working.

Here's my code:

sentence = Hello, how are you?


 print(sentence)


 print(sentence.strip())


 input(\n\nPress enter key to exit.)



Here's it's output:

Hello, how are you?
 Hello, how are you?

 Press enter key to exit.



Both results are same.

P.S.: I am using Python 3.1 IDLE on Windows 7.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor