How do I lowercase a string in Python?

python lowercase list
how to convert lowercase to uppercase in python
convert all lowercase letters to uppercase letters and vice versa. in python
python check if string contains uppercase
print only the uppercase letters in the string python
python string replace
python uppercase first letter
lowercase string

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

For example, "Kilometers" → "kilometers".

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()

Python | String lower(). lower() method converts all uppercase characters in a string into lowercase characters and returns it. Syntax : string.lower(). Parameters :. Python String lower() The string lower() method converts all uppercase characters in a string into lowercase characters and returns it.

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.

Use .lower() - For example: s = "Kilometer" print(s.lower()). The official 2.x documentation is here: str.lower() The official 3.x documentation is  In Python, to convert any string with uppercase to lowercase using a Python built-in function or method is known as lower(). This method or function lower() returns the string in lowercase if it is in uppercase else it will return the same original string itself.

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()
километр

upper() method on a string converts all of the characters to uppercase, whereas the lower() method converts all of the characters to lowercase. >>  Checking if the letter is equal to it's lowercase form: >>> c = 'a' >>> c.lower() == c True. Checking if the letter is in the list ascii_lowercase of the string module: >>> from string import ascii_lowercase >>> c = 'a' >>> c in ascii_lowercase True.

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.

The lower() method returns a string where all characters are lower case. Symbols and Numbers are ignored. Syntax. string.lower(). Parameter Values. No  Browse other questions tagged python string python-2.x uppercase lowercase or ask your own question. The Overflow Blog Podcast 236: A glitch in the Matrix

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

@Krishna, To lowercase a string in Python you can use .lower() function: x=”​Decimeter”. Print Duration: 32:15 Posted: 31 May 2019 In Python, upper () is a built-in method used for string handling. The upper () methods returns the uppercased string from the given string. It converts all lowercase characters to uppercase.If no lowercase characters exist, it returns the original string.

Special characters and numbers are unchanged because they don't have uppercase and lowercase versions. Python String lower() with user input. Let's have a  Using Python 2, calling .lower() on each string or Unicode object string1.lower() == string2.lower() will work most of the time, but indeed doesn't work in the situations @tchrist has described .

In Python, to convert any string with uppercase to lowercase using a Python built-​in function or method is known as lower(). This method or function lower() returns​  The short answer is: use the Python lower() function to make string characters to lowercase. Change from uppercase to lowercase for all the strings characters. Change from uppercase to lowercase for all the strings characters.

Convert string to lowercase in Python 3. str type since Python 3.0 contains by default Unicode characters, meaning any string like "unicode  for making lowercase from uppercase string just use "string".lower() where "string" is your string that you want to convert lowercase. for this question concern it will like this: s.lower() If you want to make your whole string variable use. s="sadf" # sadf s=s.upper() # SADF

Comments
  • 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
  • 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: километр
  • Question is how to transform string to lowercase. How this answer got so many up-votes?
  • s=s.lower() is the way to go.