Making Characters Uppercase Using Python Uppercase

The reason why python has become such a popular programming language is that it provides programmers lots and lots of versatile and standard libraries that are readily available means we don’t even need to download them explicitly. One such library in python is upper(), which converts strings in python to uppercase.  

It means if we have a string in lower letters (For example – ” hello, how are you”), we can use upper() to convert it into uppercase letters (“HELLO, HOW ARE YOU”). We will not only know how to turn all the letters to uppercase, but we will also know how to convert Only the First letter and every alternative letter to uppercase.  

Syntax  

As upper() is a built-in method we do not even need to import it. We can use it directly like this-  

string="hello" 
uppercase_string=string.upper() 
print(uppercase_string) 

Output- 
HELLO 
python uppercase

Note- It does not take any arguments.

Examples of converting string in python to uppercase 

The first letter in the string capital- 

For this purpose, we have a built-in function named capitalize() 

string="hello how are you" 
uppercase_string=string.capitalize() 
print(uppercase_string) 

Output- 
Hello how are you 
python uppercase

Sometimes we forget how the function name, so we should also know how to do the same without using these functions.  

string="hello how are you" 
new_string="" 
for i in range(len(string)): 
   if i==0: 
        new_string+=string[i].upper() 
   else: 
        new_string+=string[i] 
print(new_string) 

Output- 
Hello how are you 

The first letter of each word Capital 

Suppose we want all the words in a string to be uppercase. For this we have a method available in python called title()

string="hello how are you" 
uppercase_string=string.title() 
print(uppercase_string) 

Output- 
Hello How Are You 
python uppercase

We can do this without using built-in function – title() like this- 

string="my name is ashwini mandani" 
list1=string.split(" ") 
for i in range(len(list1)): 
   list1[i]=list1[i].capitalize() 
string=" ".join(list1) 
print(string) 

Output- 
My Name Is Ashwini Mandani 

To match if two strings are the same 

If we want to compare two strings in terms of whether they are same or not (not considering the uppercase and lowercase aspect). 

string1="thiS iS a STrinG" 
string2="ThiS iS a sTrING" 
if string1.upper()==string2.upper(): 
    print("Both the strings are same") 
else: 
    print("not same") 

Output- 
Both the strings are same 

To check if the String is already in uppercase or not. 

There are many times when we are taking input from the user and it is not compulsory that every user inputs in the same format. But we need to store the data in the same format, therefore if the string is already in uppercase print ‘already in upper case’ otherwise convert it into uppercase. 

def string_check(string): 
   if string.isupper(): 
        return("Already in UpperCase") 
   else: 
       string=string.upper() 
       return (string) 
print(string_check("MY NAME IS ASHWINI")) 
print(string_check("my name is ashwini")) 

Output- 
Already in UpperCase
MY NAME IS ASHWINI 

To make every alternative character in uppercase letters. 

string="every alternative character is in uppercase" 
new_string="" 
for i in range(len(string)): 
   if i%2==0: 
        new_string+=string[i].upper() 
   else: 
        new_string+=string[i] 
print(new_string) 

Output- 
EvErY AlTeRnAtIvE ChArAcTeR Is iN UpPeRcAsE 

Converting String to Python Uppercase without built-in function 

We can convert any string into uppercase without using any built-in function. Every character has an ASCII value. Like ‘A’ = 65, ‘B’ = 66, ‘a’ = 97, ‘b’ = 98. We can take advantage of this fact and convert lowercase characters to uppercase characters.  

Note that, the ASCII value of ‘A’ – ‘Z’ ranges from 65-90 and that of ‘a’ -’z’ range from 97-122. 

string="every charaCter is in uppercase" 
new_string="" 
for i in string: 
   # 'ord' is used to find the ascii value 
   if ord(i) >=97 and ord(i)< 123: 
       #'chr' used to find the character from ascii value 
        new_string+=chr(ord(i)-32) 
   else: 
        new_string+=i 
print(new_string) 
Output- 
EVERY CHARACTER IS IN UPPERCASE 

Conversion of String from Python Uppercase to Lowercase 

Python also provides some counterparts of its upper() and isupper() methods. In Python uppercase characters can be converted into lowercase using lower(). 

string="ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
string=string.lower() 
print(string) 

Output- 
abcdefghijklmnopqrstuvwxyz 

Similarly, if we want to check if the string is already in lowercase or not we use islower(). 

print('abcdefghijklmnopqrstuvwxyz'.islower()) 

Output- 
True 

Every other functionality works same for lower() as we have discussed in upper(). 

Must Read:

Conclusion

Python built-in functions provide a way to convert String in python from uppercase to lowercase and vice versa. Generally, when we want to keep all the text data in the same format (uppercase or lowercase), we use these methods.  

Try to run the programs on your side and let me know if you have any queries.

Happy Coding!

Leave a Reply