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.
# Quick examples of convert string to uppercase
# Example 1: Convert string to uppercase
stringVar = "welcome to sparkbyexamples"
uppercase_string = stringVar.upper()
# Example 2: Convert literal string to uppercase
uppercase_string = "welcome to sparkbyexamples".upper())
# Example 3: Convert string to uppercase
# Using upper()
stringVar = "we1c0m4 t0 5pa6k8yex0mp7e8"
uppercase_string = stringVar.upper()
# Example 4: Convert string to uppercase
stringVar = "welcome to sparkbyexamples"
uppercase_string = stringVar.upper()
# Example 5: Convert part of the string to upper
stringVar = "welcome to sparkbyexamples"
uppercase_string = stringVar[:10] + stringVar[10:].upper()
# Example 6: Convert the first letter to uppercase
# Using capitalize()
stringVar = "welcome to sparkbyexamples"
uppercase_string = stringVar.capitalize()
# Example 7: Convert part of the string
uppercase_string = stringVar[:10] + stringVar[10:].upper()
print(uppercase_string)
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,
The upper()
method returns a new string where all the characters are converted to uppercase. Keep in mind that this operation does not modify the original string; instead, it creates a new string with the uppercase characters. If you want to modify the original string in-place, you can reassign the result to the original variable.
# Create input string
stringVar = "welcome to sparkbyexamples"
print("Original string:",stringVar)
# Convert string to uppercase
uppercase_string = stringVar.upper()
print("Uppercase string:",uppercase_string)
Yields below output.
4. Convert Alphanumeric String to Upper case
Alternatively, 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.
The upper()
method effectively converts all the alphabetic characters to uppercase while leaving the numeric and other non-alphabetic characters unchanged.
# Create input string
stringVar = "we1c0m4 t0 5pa6k8yex0mp7e8"
print("Original string:",stringVar)
# Convert string to uppercase using upper()
uppercase_string = stringVar.upper()
print("Uppercase alphanumeric string:",uppercase_string)
Yields below output.
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,
# Create input string
stringVar = "welcome to sparkbyexamples"
print("Original string:",stringVar)
# Convert string to uppercase
uppercase_string = stringVar.upper()
print("Uppercase string:",uppercase_string)
# Output:
# Original string: welcome to sparkbyexamples
# Uppercase string: 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.
# Create input string
stringVar = "welcome to sparkbyexamples"
print("Original string:",stringVar)
# Convert the first letter to uppercase
# Using capitalize()
uppercase_string = stringVar.capitalize()
print("Capitalized string:",uppercase_string)
# Output:
# Original string: welcome to sparkbyexamples
# Capitalized string: 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
Frequently Asked Questions on Convert String to Uppercase
You can use the upper()
method on a string to convert all its characters to uppercase. For instance, defines a string stringVar
and then uses the upper()
method to create a new string uppercase_string
with all the characters in uppercase. The original string remains unchanged.
The upper()
method does not modify the original string; it returns a new string with all characters converted to uppercase. If you want to modify the original string in-place, you need to reassign the result to the original variable.
You can use the capitalize()
method to capitalize the first letter of a string and convert the rest to lowercase. Alternatively, you can use string slicing to maintain the case of the rest of the characters while capitalizing only the first letter.
If you want to convert only the alphabetic characters in a string to uppercase while leaving the non-alphabetic characters unchanged, you can use a combination of string manipulation and the upper()
method with a list comprehension
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 etc.
Happy Learning !!
Related Articles
- Convert String to Lowercase in Python
- Python Capitalize First Letter of String
- Compare Strings in Python
- Python Convert String to Int (Integer)
- Python Convert String to Float
- Convert String to Tuple Python
- Python Convert Bytes to String
- Convert Integer to String in Python
- How to Convert Dictionary to String Python?
- Convert JSON Object to String in Python
- Convert List of Integers to String Python
- Convert String to Dictionary Python