Convert String to Uppercase in Python

To convert a string to uppercase in Python use the upper() method. The upper() method is a built-in Python function that converts all lowercase characters in a string to uppercase and returns the resulting string.

In this article, I will explain the syntax of the python string upper() method, its parameters and explain how to convert a string to uppercase in Python.

Related: Use lower() to Convert String to Lowercase

1. Quick Examples of Convert String to Uppercase

If you are in a hurry, below are some quick examples of how to convert string to uppercase in python.


# Below are the quick examples

#Example 1: Convert string to uppercase
stringVar = "welcome to sparkbyexamples"
print(stringVar.upper())

#Example 2: Convert literal string to uppercase
result = "welcome to sparkbyexamples".upper())
print(result)

#Example 3: Convert string to uppercase
stringVar = "welcome to sparkbyexamples"
stringVar = stringVar.upper()
print(stringVar)

#Example 4: Convert alphanumeric string to uppercase 
stringVar = "we1c0m4 t0 5pa6k8yex0mp7e8"
print(stringVar.upper())

#Example 5: Convert part of the string
uppercase_string = stringVar[:10] + stringVar[10:].upper()
print(uppercase_string)

#Example 6: Convert first letter to Upper
stringVar = "welcome to sparkbyexamples"
print(stringVar.capitalize())

2. Syntax of String upper() Method

Following is the syntax for creating upper() method


# Syntax of upper() function
string.upper()

2.1 Parameter of upper()

The upper() method does not take any parameters.

3. Convert a String to Uppercase in Python

You can use the upper() function to convert characters in a string to uppercase. The upper() function returns a new string with all the characters in the original string converted to uppercase. It does not modify the original string. For example,


# Convert string to uppercase
stringVar = "welcome to sparkbyexamples"
print(stringVar.upper())

# Output
# WELCOME TO SPARKBYEXAMPLES

4. Convert Alphanumeric String to Upper case

Similarly, you can also convert alphanumeric string to uppercase using this method. For an alphanumeric string, it converts only lowercase letters to uppercase and leaves the other character unchanged.


# Convert string to uppercase using upper()
stringVar = "we1c0m4 t0 5pa6k8yex0mp7e8"
print(stringVar.upper())

# Output
# WE1C0M4 T0 5PA6K8YEX0MP7E8

5. Convert the Original Input String to Uppercase

Note that the upper() method does not modify the original string instead it returns a new string with the uppercase version of the original string. If you want to modify the original string, you can assign the result of the upper() method back to the original string. For example,


# Convert string to uppercase
stringVar = "welcome to sparkbyexamples"
result = stringVar.upper()
print(result)

# Output
# WELCOME TO SPARKBYEXAMPLES

6. Convert Part of the String to Uppercase

Sometimes you would be required to convert only part of the string, you can achieve this by splitting the string and converting part of the string to upper case, and concatenating the strings.

The below example converts only a part of the string to uppercase, Here, I used slicing and concatenation.


# Convert Part of the String to Upper
stringVar = "welcome to sparkbyexamples"
uppercase_string = stringVar[:10] + stringVar[10:].upper()
print(uppercase_string) 

# Output:
# welcome to SPARKBYEXAMPLES

7. Convert the First Letter to Upper case

You can also use the str.capitalize() method to convert the first character of a string to uppercase and the rest to lowercase.


# Convert First Letter of string to Upper
stringVar = "welcome to sparkbyexamples"
print(stringVar.capitalize())

# Output: 
# Welcome to sparkbyexamples

8. Comparison of Strings Using upper() Method

Similarly, you can use the upper() method to compare two strings for equality, ignoring the case of the characters. This function is used to convert both strings to uppercase before the comparison.


# Comparison of strings using upper() method
String1 = "welcome to sparkbyexamples"
String2 ="WeLComE TO SpArKbYExAmPlEs"
 
if(String1.upper() == String2.upper()):
    print("Strings are same")
else:
    print("Strings are not same")

# Output
# Strings are same

Conclusion

In this article, I have explained the Python string upper() method syntax, parameters, and how to convert all lowercase characters in a string to uppercase and also explained several examples like converting title case, converting on original string e.t.c.

Happy Learning !!

Leave a Reply

You are currently viewing Convert String to Uppercase in Python