Is there a .tolower() for a string??

Peter Otten __peter__ at web.de
Wed Nov 26 14:27:07 EST 2003


Amy wrote:

> I have a string say
> 
> a = "Hello how are YOU"
> 
> I want to end up with
> 
> a = "hello how are you'
> 
> Isn't there a built in method for changing a string to lowercase?

dir("")

will give you all str attributes. The most promising is lower, so let's try:

>>> a = "LOWER"
>>> a.lower()
'lower'
>>> a
'LOWER'

You might not have expected the last outcome. This is because the lower()
method does not change the original string but creates a new one that is
all lowercase. Strings are "immutable", i. e. cannot be changed once they
are created. To get the desired effect, you need to "rebind" a:

>>> a = a.lower()
>>> a
'lower'

Peter




More information about the Python-list mailing list