Using the Python upper() Method

How to Use the Python upper() Method

The upper() method is a built-in function in Python that converts all the lowercase characters in a string to uppercase. It returns a new string with all the lowercase characters replaced by their corresponding uppercase characters. The upper() method does not modify the original string; instead, it creates a new string with the modified characters.

How to Use the upper() Method?

To use the upper() method, you first need to define a string variable. Then, you can call the upper() method on the string variable to convert all the lowercase characters to uppercase. Here is an example:

# Example 1
string = "hello, world!"
upper_string = string.upper()
print(upper_string)

# Output: HELLO, WORLD!

In the above example, we defined a string variable string with the value “hello, world!”. We then called the upper() method on the string variable and assigned the result to a new variable upper_string. Finally, we printed the upper_string variable, which contains the modified string with all the lowercase characters converted to uppercase.

You can also call the upper() method directly on a string literal. Here is an example:

# Example 2
string = "hello, world!".upper()
print(string)

# Output: HELLO, WORLD!

In this example, we called the upper() method directly on the string literal “hello, world!”.

More Examples

Here are some more examples to illustrate the usage of the upper() method:

# Example 3
string = "python"
upper_string = string.upper()
print(upper_string)

# Output: PYTHON
# Example 4
string = "This is a sentence."
upper_string = string.upper()
print(upper_string)

# Output: THIS IS A SENTENCE.
# Example 5
string = "123abc"
upper_string = string.upper()
print(upper_string)

# Output: 123ABC
# Example 6
string = ""
upper_string = string.upper()
print(upper_string)

# Output: (empty string)

Conclusion

In this article, we have discussed the upper() method in Python, which is used to convert all the lowercase characters in a string to uppercase. We have provided multiple examples to illustrate the usage of the upper() method. The upper() method is a useful function in Python when working with strings, and it is essential to understand how to use it effectively.