How to use upper() Function in Python with Example?

The `upper()` function in Python is a string method used to convert all lowercase characters in a string to uppercase. It doesn’t modify the original string but returns a new string with all characters converted to uppercase.

Syntax

string.upper()

Example

Here’s a simple example to illustrate the use of the `upper()` function:

# Original string
text = "hello, world!"

# Convert the string to uppercase
uppercase_text = text.upper()

# Display the result
print(uppercase_text)

Output

HELLO, WORLD!

Explanation

– Original String: `text = “hello, world!”`
– This is the string you want to convert to uppercase.
– Using `upper()`: `uppercase_text = text.upper()`
– This method converts all the lowercase letters in `text` to uppercase, resulting in `”HELLO, WORLD!”`.

– Result: The `print(uppercase_text)` statement outputs the converted string, which is all in uppercase.

Additional Example

# Another example
greeting = "Welcome to Python Programming!"
# Convert to uppercase
uppercase_greeting = greeting.upper()
# Display the result
print(uppercase_greeting)

Output

WELCOME TO PYTHON PROGRAMMING!

In this example, the `upper()` function converts the entire string `greeting` into uppercase. This method is useful when you need to ensure that all characters in a string are in uppercase, regardless of their original case.