In Python, we can use built-in functions to manipulate strings. For example, we may want to capitalize the first characters in a name for form entry. The upper() function is helpful for converting all case-based characters in a string to uppercase. We can use the function isupper() to check if characters in a string are uppercase.
This tutorial will go through how to use the upper() and isupper() functions with examples and alternative ways to capitalize characters in a string.
Table of contents
- What Is a Python String? Refresher
- How to Use Python upper()
- How to Python isUpper()
- Combining upper() and isUpper()
- Alternatives to upper()
- Converting Python Strings from Uppercase to Lowercase using lower()
What Is a Python String? Refresher
A string is a list of characters in Unicode representation, and Python deals with a string as an array of characters. Therefore a string is indexable, and we can access a character or several characters in a string using indexing and slicing.
We can declare strings in Python using singe quote ‘ ‘ or double quotes ” “. Let’s look at an example of declaring a string and printing the value to the console.
yoda_string = "A Python string this is" print(yoda_string)
A Python string this is
The program returns A Python string this is to the console.
Now that we have refreshed on Python strings, we can look at Python upper() and isUpper().
How to Use Python upper()
The built-in Python method upper() converts all case-based characters within a string to uppercase. The method returns a copy of the original string, with all characters in uppercase. The syntax of the upper() method is:
Let’s look at an example of using the upper() method on a string. We will write a program that takes a name and converts it to uppercase:
name = "Han Solo" print(name.upper())
The above code declares the variable: name, which stores the string “Han Solo”. The upper() method converts the name to uppercase, and we print the resultant string to the console. The output from the code is as follows:
The upper() method does not affect symbols, numbers, or whitespace in a string because these characters are not case-based.
How to Python isUpper()
The built-in Python method isupper() evaluates a string and returns True if all the characters within the string are uppercase. Otherwise, it returns False. The method does not check numbers, symbols, and spaces as these characters are not text-based. The syntax of the isupper() method is:
Let’s look at an example of using the isupper() method to determine if several strings have all uppercase letters or not.
a = "research scientist" b = "Research Scientist" c = "RESEARCH SCIENTIST" print(a.isupper()) print(b.isupper()) print(c.isupper())
Let’s run the code to what happens:
False False True
String “a” and “b” return false because not all characters are uppercase. String “c” returns true because all of the characters are uppercase.
Combining upper() and isUpper()
We can combine upper() and isUpper() to do more subtle string manipulation. There are times when taking input from the user needs to be consistent, for example, string comparison. Let’s write a function that checks if a string is already uppercase. If the string is not uppercase, we convert it to uppercase using upper() and return it to the user. The code will look as follows:
def check_string(string): if string.isupper(): return("String is already all uppercase") else: string = string.upper() return string
Let’s test this check_string() function on two strings:
print(check_string("RESEARCH SCIENTIST")) print(check_string("research SCIENTIST"))
String is already all uppercase RESEARCH SCIENTIST
The program converts the second string to all uppercase because the first part is lowercase. The first string has all uppercase letters, and therefore the program returns the output “String is already all uppercase”.
Alternatives to upper()
Additional built-in methods can perform similar operations to upper(). Let’s look at some of the other methods.
Capitalize First Letter in String using capitalize()
We can use the built-in method capitalize() to convert the first letter in a string to uppercase. Let’s look at an example with a string.
string = "hello world" capitalized_first_letter = string.capitalize() print(capitalized_first_letter)
Capitalize First Letter in each Word of String using title()
We may want to capitalize the first letter in each word — for example, the title of a document. We can use the built-in method title(). Let’s look at an example with a string.
string = "hello world" title_string = string.title() print(title_string)
Make Every Other Character In a String Uppercase
Let’s look at an example where we want every other character in a string to be upper case. We can use a combination of a for loop and the upper() method.
string="this string has every other character 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)
In the above code, we do the following:
- We define a string with all lowercase characters.
- We then define a second empty string to store the values returned from the for loop.
- The for loop iterates over the characters in the string.
- An if-else statement defines two operations depending on the position of a character in the string.
- We use the modulo operator to get the even indices of the first string and convert the characters to uppercase at those indices. We concatenate the characters to the new_string.
- The characters at odd indices are not made uppercase. We also concatenate these characters to the new_string.
- Once the for loop has completed, the program prints the resultant string, which is:
ThIs sTrInG HaS EvErY OtHeR ChArAcTeR In uPpErCaSe
We can see every other character is in uppercase. Note that white space in a string also has an index.
Converting Python Strings from Uppercase to Lowercase using lower()
Python provides built-in methods to convert strings to all lowercase and check if all characters in a string are lowercase. The method names are lower() and islower(). Let’s look at an example of using lower():
string = "This is A STring" lower_string = string.lower() print(lower_string)
this is a string
The original string has a combination of upper and lowercase characters. The lower() method returns a string with all lowercase characters. Similar to isupper(), we can use islower() to check if a string has all lowercase characters:
string = "python is fun to learn" print(string.islower())
The method islower() evaluates the string and returns True because all characters were lowercase.
Congratulations on reading to the end of this tutorial! Converting strings to uppercase is a common string operation in Python. We can use the upper() method to convert a string to all uppercase characters and use the isupper() method to check whether a string is already in uppercase. We can use other built-in methods to capitalize individual characters, namely, capitalize() and title().
The counterparts to upper() and isupper() are lower() and islower(), which convert strings to lowercase and check if a string is already in lowercase, respectively. Generally, when we want to keep text data in the same format, whether uppercase or lowercase, we can use the upper() and lower() methods.
For further reading on strings, go to the following articles:
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!