Upper case | How To Change A String Into Upper case?

Here in this tutorial, we will learn more about How to change a string into Upper case which could be done using different ways like using the inbuild function and by adding the number 26 to the ASCII values of lowercase letters as the ASCII(American Standard Code for Information Interchange) values of the upper case start from 98 and end at 122. 

Upper case

Upper case

At times we need to convert the existing string into the upper case which we can perform in two different ways that we will discuss one by one here.

Converting Using the Inbuilt function

As we know in python we have a lot of inbuilt functions which we can directly use them perform our operations and get our tasks done same we are just going to follow the code given below where we simply used the upper() function to convert every character of string into upper case.

s = 'Hello, World!'
s_ uppercase = s.upper()
print(s_ uppercase) # Output: HELLO, WORLD!

here the given text in the variable s will be converted into the upper case.

Making the string into upperCase using ASCII Values

As we know each character on a keyword is marked with a unique number that we call an ASCII number and lowercase has different ASCII value And we can use the ASCII values to convert all the characters of a string into Upper case.

And to convert it follow the example code given below and learn.

def to_ uppercase_ascii(s):
    result = ''
    for char in s:
        if 97 <= ord(char) <= 122: # Check if the character is a lowercase letter
            result += chr(ord(char) - 32) # Convert the ASCII value to uppercase
            result += char # Append the character as is
    return result

# Example usage
s = 'Hello, World!'
s_uppercase = to_ uppercase_ascii(s)
print(s_ uppercase) # Output: HELLO, WORLD!


To learn more about converting a string into uppercase follow the link given Uppercase Conversion of whole String in one go.


To learn more about python visit.Some Basic Python String Programs-2.

Leave a Comment

%d bloggers like this: