Python 3 – String upper() Method
In Python, strings are a widely used data type, and they come with a bunch of built-in methods to simplify string manipulation. One such method is the upper() method, which returns a copy of the original string with all lowercase letters converted to uppercase.
Syntax:
To use the upper() method in Python, you just need to call it on a string object like this:
string.upper()
Where string
is the string you want to convert to uppercase.
Example:
>>> text = "hello world"
>>> print(text.upper())
HELLO WORLD
In the above example, we created a string object called text
and used the upper()
method to convert it to uppercase. The result was then printed using the print()
function.
Multiple Strings:
The upper() method can also be used to convert multiple strings to uppercase. To do this, you can use a loop to iterate through a list of strings and apply the method to each one.
Example:
>>> texts = ["hello world", "how are you", "good morning"]
>>> for text in texts:
... print(text.upper())
...
HELLO WORLD
HOW ARE YOU
GOOD MORNING
In the above example, we created a list called texts
that contains three string objects. We then used a for
loop to loop through each string in the list and apply the upper()
method to it. The result was then printed using the print()
function.
Non-ASCII Characters:
The upper() method can handle non-ASCII characters, such as accented letters or characters from other languages. This makes the method particularly useful in multilingual applications.
Example:
>>> text = "café au lait"
>>> print(text.upper())
CAFÉ AU LAIT
In the above example, we created a string object called text
that contains the French phrase “café au lait”. We then used the upper() method to convert it to uppercase. The method correctly handled the non-ASCII character é and produced the correct result.
Other Methods:
There are several other methods in Python that are similar to upper() and can also be used to manipulate string cases. Some of these methods include:
lower()
– returns a copy of the original string with all uppercase letters converted to lowercase.capitalize()
– returns a copy of the original string with only the first letter capitalized.title()
– returns a copy of the original string with the first letter of each word capitalized.
Take a look at the example below to see how these methods work:
>>> text = "hELlo wORLd"
>>> print(text.lower())
hello world
>>> print(text.capitalize())
Hello world
>>> print(text.title())
Hello World
In the above example, we created a string object called text
that contains a mixed case phrase. We then used the lower()
, capitalize()
, and title()
methods to convert the string to lowercase, change the first letter to uppercase, and capitalize the first letter of each word, respectively.
Conclusion
In conclusion, the upper() method is a handy Python string method that can be used to convert all lowercase letters in a string to uppercase. It’s easy to use and can handle non-ASCII characters, making it a great choice for multilingual applications. Additionally, there are several other string methods in Python that can be used to manipulate string cases, including lower()
, capitalize()
, and title()
.