By Benjamin Didur


2011-07-23 03:08:53 8 Comments

Is there a way to convert a string from uppercase, or even part uppercase to lowercase?

E.g. Kilometers --> kilometers.

6 comments

@Petar Ivanov 2011-07-23 03:09:58

Use .lower() - For example:

s = "Kilometer"
print(s.lower())

The official 2.x documentation is here: str.lower()
The official 3.x documentation is here: str.lower()

@Aaron Hall 2015-07-23 22:40:45

How to convert string to lowercase in Python?

Is there any way to convert an entire user inputted string from uppercase, or even part uppercase to lowercase?

E.g. Kilometers --> kilometers

The canonical Pythonic way of doing this is

>>> 'Kilometers'.lower()
'kilometers'

However, if the purpose is to do case insensitive matching, you should use case-folding:

>>> 'Kilometers'.casefold()
'kilometers'

Here's why:

>>> "Maße".casefold()
'masse'
>>> "Maße".lower()
'maße'
>>> "MASSE" == "Maße"
False
>>> "MASSE".lower() == "Maße".lower()
False
>>> "MASSE".casefold() == "Maße".casefold()
True

This is a str method in Python 3, but in Python 2, you'll want to look at the PyICU or py2casefold - several answers address this here.

Unicode Python 3

Python 3 handles plain string literals as unicode:

>>> string = 'Километр'
>>> string
'Километр'
>>> string.lower()
'километр'

Python 2, plain string literals are bytes

In Python 2, the below, pasted into a shell, encodes the literal as a string of bytes, using utf-8.

And lower doesn't map any changes that bytes would be aware of, so we get the same string.

>>> string = 'Километр'
>>> string
'\xd0\x9a\xd0\xb8\xd0\xbb\xd0\xbe\xd0\xbc\xd0\xb5\xd1\x82\xd1\x80'
>>> string.lower()
'\xd0\x9a\xd0\xb8\xd0\xbb\xd0\xbe\xd0\xbc\xd0\xb5\xd1\x82\xd1\x80'
>>> print string.lower()
Километр

In scripts, Python will object to non-ascii (as of Python 2.5, and warning in Python 2.4) bytes being in a string with no encoding given, since the intended coding would be ambiguous. For more on that, see the Unicode how-to in the docs and PEP 263

Use Unicode literals, not str literals

So we need a unicode string to handle this conversion, accomplished easily with a unicode string literal, which disambiguates with a u prefix (and note the u prefix also works in Python 3):

>>> unicode_literal = u'Километр'
>>> print(unicode_literal.lower())
километр

Note that the bytes are completely different from the str bytes - the escape character is '\u' followed by the 2-byte width, or 16 bit representation of these unicode letters:

>>> unicode_literal
u'\u041a\u0438\u043b\u043e\u043c\u0435\u0442\u0440'
>>> unicode_literal.lower()
u'\u043a\u0438\u043b\u043e\u043c\u0435\u0442\u0440'

Now if we only have it in the form of a str, we need to convert it to unicode. Python's Unicode type is a universal encoding format that has many advantages relative to most other encodings. We can either use the unicode constructor or str.decode method with the codec to convert the str to unicode:

>>> unicode_from_string = unicode(string, 'utf-8') # "encoding" unicode from string
>>> print(unicode_from_string.lower())
километр
>>> string_to_unicode = string.decode('utf-8') 
>>> print(string_to_unicode.lower())
километр
>>> unicode_from_string == string_to_unicode == unicode_literal
True

Both methods convert to the unicode type - and same as the unicode_literal.

Best Practice, use Unicode

It is recommended that you always work with text in Unicode.

Software should only work with Unicode strings internally, converting to a particular encoding on output.

Can encode back when necessary

However, to get the lowercase back in type str, encode the python string to utf-8 again:

>>> print string
Километр
>>> string
'\xd0\x9a\xd0\xb8\xd0\xbb\xd0\xbe\xd0\xbc\xd0\xb5\xd1\x82\xd1\x80'
>>> string.decode('utf-8')
u'\u041a\u0438\u043b\u043e\u043c\u0435\u0442\u0440'
>>> string.decode('utf-8').lower()
u'\u043a\u0438\u043b\u043e\u043c\u0435\u0442\u0440'
>>> string.decode('utf-8').lower().encode('utf-8')
'\xd0\xba\xd0\xb8\xd0\xbb\xd0\xbe\xd0\xbc\xd0\xb5\xd1\x82\xd1\x80'
>>> print string.decode('utf-8').lower().encode('utf-8')
километр

So in Python 2, Unicode can encode into Python strings, and Python strings can decode into the Unicode type.

@bballdave025 2018-07-23 17:08:31

This is the most complete, portable answer. In my opinion, it should be the correct one. Thanks especially for the, "However, if the purpose is to do case insensitive matching, you should use case-folding" +1

@bballdave025 2018-07-23 17:27:30

I have one note that doesn't necessarily apply to the OP's question, but which is important with portability (internationalization) when doing case insensitive matching. With case-insensitive matching, diacritics (accent marks) may become a concern. Example: >>> "raison d'être".casefold(); "raison d'être" Check out this answer about unidecode

@U9-Forward 2018-11-07 05:27:18

Don't try this, totally un-recommend, don't do this:

import string
s='ABCD'
print(''.join([string.ascii_lowercase[string.ascii_uppercase.index(i)] for i in s]))

Output:

abcd

Since no one wrote it yet you can use swapcase (so uppercase letters will become lowercase, and vice versa) (and this one you should use in cases where i just mentioned (convert upper to lower, lower to upper)):

s='ABCD'
print(s.swapcase())

Output:

abcd

@user735977 2011-07-23 04:34:10

You can do what Peter said, or if you want the user to input something you could do the below code:

raw_input('Type Something').lower()

It will then automatically convert the string they typed into lowercase.

Note: raw_input was renamed to input in Python 3.x and above.

@Félix Saparelli 2016-06-04 07:40:17

Note that there is no "or" about this, it's not a different technique. input() returns a string, which you can then apply string methods onto, just like Petar demonstrated.

@ppperry 2017-07-19 22:19:14

Unnecessary expansion of question; this is just about the lowercase and not the input

@Vladimir Gorovoy 2014-10-03 08:02:17

With Python 2, this doesn't work for non-English words in UTF-8. In this case decode('utf-8') can help:

>>> s='Километр'
>>> print s.lower()
Километр
>>> print s.decode('utf-8').lower()
километр

@bballdave025 2018-07-23 17:16:25

Perhaps we should be a bit more explicit by saying that the decode('utf-8') is not only unnecessary in Python 3, but causes an error. (ref). Example: $python3; >>>s='Километр'; >>>print (s.lower); #result: километр >>>s.decode('utf-8').lower(); #result: ...AttributeError: 'str' object has no attribute 'decode' We can see a second way to do this, referencing the excellent answer of @AaronHall. >>>s.casefold() #result: километр

@ergo 2013-11-18 18:35:07

Also, you can overwrite some variables:

s = input('UPPER CASE')
lower = s.lower()

If you use like this:

s = "Kilometer"
print(s.lower())     - kilometer
print(s)             - Kilometer

It will work just when called.

@Munim Munna 2018-05-09 19:18:23

Question is how to transform string to lowercase. How this answer got so many up-votes?

@m00lti 2018-08-07 11:03:30

s=s.lower() is the way to go.

Related Questions

Sponsored Content

12 Answered Questions

[SOLVED] Is there a way to substring a string?

  • 2009-03-19 17:29:41
  • Joan Venge
  • 2358927 View
  • 1767 Score
  • 12 Answer
  • Tags:   python string

46 Answered Questions

[SOLVED] How to replace all occurrences of a string in JavaScript

48 Answered Questions

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

26 Answered Questions

18 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

57 Answered Questions

[SOLVED] What is the difference between String and string in C#?

15 Answered Questions

[SOLVED] What are metaclasses in Python?

20 Answered Questions

[SOLVED] How to convert a string to lower case in Bash?

27 Answered Questions

[SOLVED] Random string generation with upper case letters and digits in Python

  • 2010-02-13 12:23:58
  • Hellnar
  • 718411 View
  • 1120 Score
  • 27 Answer
  • Tags:   python string random

Sponsored Content