Built with Tutify.io

    AIStudyLabs

    Convert String to Uppercase in Python

    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

    <a id="h-1-quick-examples-of-convert-string-to-uppercase"></a>

    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 aistudylabs"
    uppercase_string = stringVar.upper()
    
    # Example 2: Convert literal string to uppercase
    uppercase_string = "welcome to aistudylabs".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 aistudylabs"
    uppercase_string = stringVar.upper()
    
    # Example 5: Convert part of the string to upper
    stringVar = "welcome to aistudylabs"
    uppercase_string = stringVar[:10] + stringVar[10:].upper()
    
    # Example 6: Convert the first letter to uppercase 
    # Using capitalize()
    stringVar = "welcome to aistudylabs"
    uppercase_string = stringVar.capitalize()
    
    # Example 7: Convert part of the string
    uppercase_string = stringVar[:10] + stringVar[10:].upper()
    print(uppercase_string)
    

    <a id="h-2-syntax-of-string-upper-method"></a>

    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.

    <a id="h-3-convert-a-string-to-uppercase-in-python"></a>

    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 aistudylabs"
    print("Original string:",stringVar)
    
    # Convert string to uppercase
    uppercase_string = stringVar.upper()
    print("Uppercase string:",uppercase_string)
    

    Yields below output.

    <a id="h-4-convert-alphanumeric-string-to-upper-case"></a>

    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 aistudylabs"
    print("Original string:",stringVar)
    
    # Convert string to uppercase
    uppercase_string = stringVar.upper()
    print("Uppercase string:",uppercase_string)
    
    # Output:
    # Original string: welcome to aistudylabs
    # Uppercase string: WELCOME TO aistudylabs
    

    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 aistudylabs"
    uppercase_string = stringVar[:10] + stringVar[10:].upper()
    print(uppercase_string) 
    
    # Output:
    # welcome to aistudylabs
    

    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 aistudylabs"
    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 aistudylabs
    # Capitalized string: Welcome to aistudylabs
    

    <a id="h-8-comparison-of-strings-using-upper-method"></a>

    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 aistudylabs"
    String2 ="WeLComE TO aistudylabs"
     
    if(String1.upper() == String2.upper()):
        print("Strings are same")
    else:
        print("Strings are not same")
    
    # Output
    # Strings are same
    

    <a id="h-frequently-asked-questions-on-convert-string-to-uppercase"></a>

    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 !!

    <a id="h-related-articles"></a>

    Related Articles