Python str.upper() function
str.upper()
Function in Python
The str.upper()
function in Python is used to convert all lowercase letters in a string to uppercase. It leaves non-alphabetical characters (such as digits, punctuation, and spaces) unchanged.
Syntax:
string
: The string that you want to convert to uppercase.
Example:
In this example:
- The string
"hello, world!"
is converted to"HELLO, WORLD!"
. - The function changes only the lowercase letters to uppercase, while punctuation and spaces remain unaffected.
Example with mixed case:
Here, the lowercase letters in "Python is Fun!"
are converted to uppercase, while the uppercase letters and other characters remain unchanged.
Example with numbers and special characters:
In this case:
- The digits (
123
) and special characters (!@#
) are not affected. - The lowercase letters (
abc
) are converted to uppercase (ABC
).
Key Points:
str.upper()
only affects alphabetic characters and converts them to uppercase.- It does not modify the original string but returns a new string with the changes.
- Non-alphabetic characters like numbers, punctuation, and whitespace remain unchanged.