Skip to main content

How to convert a string to uppercase in Python

How to convert a string to uppercase in Python.

Here is a step-by-step tutorial on how to convert a string to uppercase in Python:

Step 1: Start by defining the string that you want to convert to uppercase. For example, let's say you have a string called my_string:

my_string = "Hello, World!"

Step 2: Use the upper() method to convert the string to uppercase. This method returns a new string where all the characters are in uppercase. Assign the result to a new variable, or overwrite the original variable if you want to update it:

upper_string = my_string.upper()

In this case, the value of upper_string will be "HELLO, WORLD!".

Step 3: Print or use the converted string as desired. For example, you can simply print it using the print() function:

print(upper_string)

This will output "HELLO, WORLD!".

Alternatively, you can use the converted string in any other operations or assignments that you need.

Here's the complete code:

my_string = "Hello, World!"
upper_string = my_string.upper()
print(upper_string)

Output:

HELLO, WORLD!

That's it! You have successfully converted a string to uppercase in Python.