How to Convert Strings to Uppercase in Python?

Estimated read time 1 min read

In Python, you can convert strings to uppercase using the upper() method or the str.upper() function. Here’s how you can do it:

Using the upper() method:

string = "hello, world!"
uppercase_string = string.upper()
print(uppercase_string)  # Output: "HELLO, WORLD!"

In this example, the upper() method is called on the string variable, which returns a new string with all characters converted to uppercase. The resulting uppercase string is then assigned to the uppercase_string variable and printed.

Using the str.upper() function:

string = "hello, world!"
uppercase_string = str.upper(string)
print(uppercase_string)  # Output: "HELLO, WORLD!"

In this example, the str.upper() function is called with the string variable as an argument. It returns a new string with all characters converted to uppercase, which is then assigned to the uppercase_string variable and printed.

Both the upper() method and the str.upper() function provide the same result of converting a string to uppercase. The choice between them depends on whether you prefer the method syntax or the function syntax.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply