- 7 Ways of Making Characters Uppercase Using Python
- Syntax of Making Characters Uppercase Using Python
- Examples of converting string in python uppercase
- 1. The First Letter in the string capital in Python
- 2. The First Letter of each word Capital in Python
- 3. To match if two strings are the same
- 4. To check if the String is already in uppercase or not
- 5. To make every alternative character in uppercase letters
- 6. Converting String to Python Uppercase without built-in function
- 7. Conversion of String from Python Uppercase to Lowercase
- Must Read:
- Conclusion
- Python Uppercase - All you need to know
- Table of Contents - Python Uppercase
- An introduction to Python uppercase
- Syntax and parameters
- Input:
- Output:
- Input:
- Output:
- Python isupper with examples
- Input:
- Output:
- Closing thoughts
7 Ways of Making Characters Uppercase Using Python
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 install 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 of Making Characters Uppercase Using Python
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)
Note- It does not take any arguments.
Examples of converting string in python uppercase
- The first letter in the string capital
- First letter of each word Capital
- To match if two strings are the same
- Check if the String is already in uppercase or not
- To make every alternative character in uppercase letters
- Converting String to Python Uppercase without built-in function
- Conversion of String from Python Uppercase to Lowercase
1. The First Letter in the string capital in Python
For this purpose, we have a built-in function named capitalize()
string="hello how are you" uppercase_string=string.capitalize() print(uppercase_string)
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)
2. The First Letter of each word Capital in Python
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)
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
3. 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
4. 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
5. 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
6. 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
7. 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())
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!
Python Uppercase - All you need to know
In this short tutorial, we are going to look into what Python’s uppercase methods are. We will also consider some examples to clearly understand the concepts.
Table of Contents - Python Uppercase
An introduction to Python uppercase
While working with strings, there might be instances where we need to convert lowercase strings to uppercase. Or we might have to check if a given string is uppercase or not. Python facilitates this with the following functions:
The Python upper() method is used to convert lowercase letters in a string to uppercase. The isupper() method, on the other hand, returns True if all the letters in a string are uppercase.
A real-life use-case of this function is an amusement park ticketing application. You may want to convert all the names of the visitors to uppercase format before printing them on the ticket. This helps with uniformity in storage and easy readability during the ticket check process.
Syntax and parameters
The syntax of the method upper() is:
As you can see, the method does not take any parameters. In the place of string we enter the name of the variable containing the string.
Input:
# Python program to show the working of the upper() function visitor007 = 'Flexiple' print("Original String:") print(visitor007) # upper() function converts string to uppercase print("\nModified String:") print(visitor007.upper())
Output:
Original String: Flexiple Modified String: FLEXIPLE
In the above code snippet, we declare a variable called visitor007 . This variable stores the name of our amusement park visitor. Secondly, we use Python’s upper() method to convert the visitor007 to uppercase. Finally, the revised name is printed.
The upper() method does not affect whitespace, numbers or symbols with a string. This is because those characters aren’t upper/lower case based. Let’s look at an example for the same.
Input:
# Python program for upper() function visitor007 = 'F1exip1e' print("The Original String:") print(visitor007) # upper() function converts string to uppercase print("\nModified String:") print(visitor007.upper())
Output:
The Original String: F1exip1e Modified String: F1EXIP1E
Python isupper with examples
Before we do convert a string to uppercase, we might have to check if it is already in uppercase . The isupper() method checks every case-based character. If it finds any characters in lowercase it returns False, else it returns True.
Input:
visitor_name001 = "Flexiple" print(visitor_name001.isupper()) visitor_name002 = "FLEXIPLE" print(visitor_name002.isupper())
Output:
In the output of our first print statement, the method has returned False . This is because the function found lowercase characters in the string. Whereas, the second print statement has printed out True . This is because the string contains only uppercase letters.
Closing thoughts
Switching between the character cases is a common string operation in Python. In this Python tutorial, we looked at how the upper() method is used. We learned how the isupper() method is used to check a given string. We also looked into a couple of examples to understand both the methods completely.