The lower[]
method converts all uppercase characters in a string into lowercase characters and returns it.
Example
message = 'PYTHON IS FUN'
# convert message to lowercase
print[message.lower[]]
# Output: python is fun
Syntax of String lower[]
The syntax of lower[]
method is:
string.lower[]
lower[] Parameters[]
lower[]
method doesn't take any parameters.
lower[] Return value
lower[]
method returns the lowercase string from the given string. It converts all uppercase characters to lowercase.
If no uppercase characters exist, it returns the original string.
Example 1: Convert a string to lowercase
# example string
string = "THIS SHOULD BE LOWERCASE!"
print[string.lower[]]
# string with numbers
# all alphabets should be lowercase
string = "Th!s Sh0uLd B3 L0w3rCas3!"
print[string.lower[]]
Output
this should be lowercase! th!s sh0uld b3 l0w3rcas3!
Example 2: How lower[] is used in a program?
# first string
firstString = "PYTHON IS AWESOME!"
# second string
secondString = "PyThOn Is AwEsOmE!"
if[firstString.lower[] == secondString.lower[]]:
print["The strings are same."]
else:
print["The strings are not same."]
Output
The strings are same.
Note: If you want to convert to uppercase string, use upper[]. You can also use swapcase[] to swap between lowercase to uppercase.
Hàm ljust[] trong Python
Hàm lstrip[] trong Python
Hàm lower[] trong Python chuyển đối tất cả chữ hoa trong chuỗi sang kiểu chữ thường.
Nội dung chính
- Cú pháp
- Ví dụ hàm lower[] trong Python
Cú pháp
Cú pháp của lower[] trong Python:
str.lower[]
Ví dụ hàm lower[] trong Python
Ví dụ sau minh họa cách sử dụng của hàm lower[] trong Python.
str1 = "Vi Du Ham lower[] Trong Python"; print [str1.lower[]]
Chạy chương trình Python trên sẽ cho kết quả −
Output:
vi du ham lower[] trong python
Hàm ljust[] trong Python
Hàm lstrip[] trong Python
The lower[]
method converts all uppercase characters in a string into lowercase characters and returns it.
Nội dung chính
- Syntax of String lower[]
- lower[] Parameters[]
- lower[] Return value
- Example 1: Convert a string to lowercase
- Example 2: How lower[] is used in a program?
- Unicode Python 3
- Python 2, plain string literals are bytes
- Use Unicode literals, not str literals
- Best Practice, use Unicode
- Can encode back when necessary
- How do you make a lower case in Python?
- What is the lower [] function in Python?
- How do you make a string lowercase?
- How do you change case in Python?
Example
message = 'PYTHON IS FUN'
# convert message to lowercase
print[message.lower[]]
# Output: python is fun
Syntax of String lower[]
The syntax of lower[]
method is:
string.lower[]
lower[] Parameters[]
lower[]
method doesn't take any parameters.
lower[] Return value
lower[]
method returns the lowercase string from the given string. It converts all uppercase characters to lowercase.
If no uppercase characters exist, it returns the original string.
Example 1: Convert a string to lowercase
# example string
string = "THIS SHOULD BE LOWERCASE!"
print[string.lower[]]
# string with numbers
# all alphabets should be lowercase
string = "Th!s Sh0uLd B3 L0w3rCas3!"
print[string.lower[]]
Output
this should be lowercase! th!s sh0uld b3 l0w3rcas3!
Example 2: How lower[] is used in a program?
# first string
firstString = "PYTHON IS AWESOME!"
# second string
secondString = "PyThOn Is AwEsOmE!"
if[firstString.lower[] == secondString.lower[]]:
print["The strings are same."]
else:
print["The strings are not same."]
Output
The strings are same.
Note: If you want to convert to uppercase string, use upper[]. You can also use swapcase[] to swap between lowercase to uppercase.
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.
How do you make a lower case in Python?
The . lower[] method takes no arguments and returns the lowercased strings from the given string by converting each uppercase character to lowercase. If there are no uppercase characters in the given string, it returns the original string.
What is the lower [] function in Python?
The lower[] method returns a string where all characters are lower case.
How do you make a string lowercase?
JavaScript String toLowerCase[] The toLowerCase[] method converts a string to lowercase letters. The toLowerCase[] method does not change the original string.
How do you change case in Python?
Python string | swapcase[] The string swapcase[] method converts all uppercase characters to lowercase and vice versa of the given string, and returns it. Here string_name is the string whose cases are to be swapped.