python string uppercase

Python String Uppercase – With Examples

In python, the built-in string function upper() is used to convert a string to uppercase. It converts all lowercase characters in a string to uppercase and returns it.

python string uppercase
s = "this is sparta!"
print(s.upper())

Output:

THIS IS SPARTA!

  • Syntax
  • Examples
  • How to check if a python string is uppercase or not?

The following is the syntax for using the string upper() function:

string.upper()

Parameters:

The string upper function does not take any parameters.

Returns:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

A string with all the cased characters converted to uppercase.

Example 1: Convert string with a mix of lowercase and uppercase characters to uppercase.

s = "tHiS iS sPArTa!"
# original string
print("Original String: ", s)
# after converting to uppercase
s_upper = s.upper()
print("After Converting:", s_upper)

Output:

Original String:  tHiS iS sPArTa!
After Converting: THIS IS SPARTA!

In the above example, all the lowercase characters have been converted to uppercase while the uppercase characters and non-cased characters remain unchanged.

Example 2: Convert string without any alphabetical character to uppercase.

s = "123 #$% <>!?"
# original string
print("Original String: ", s)
# after converting to uppercase
s_upper = s.upper()
print("After Converting:", s_upper)

Output:

Original String:  123 #$% <>!?
After Converting: 123 #$% <>!?

In the above example, since the string did not have any cased characters (that is, characters associated with a case like lowercase, uppercase, or titlecase) we get the same string without any changes on applying the upper() function.

Example 3: Using the python string uppercase function to match strings.

Often it’s required to check if two strings are the same irrespective of their case. For example, you have the names of all your leads collected via a form on your website, and it’s quite possible that many of them didn’t use a style convention like using titlecase (e.g. John Doe ), some may have filled it in all lower or all uppercase as well. In such a scenario, you can match the two strings by converting both to either upper, lower, or titlecase.

# matching strings
# list of names 
ls = ["Bruce Banner", "peter Parkar", "STEVE RODERS"]
# name to match for
name = "Peter Parkar"
# check whether the name is present in the list
for l in ls:
    # match for the name
    if l.upper() == name.upper():
        print("Match found for", name)

Output:

Match found for Peter Parkar

In the above example, we check whether a name is present in a list irrespective of the case of the strings.

We can check whether a string is uppercase or not using the isupper() function. It’s a string function that returns True if all the cased characters in the string are in uppercase. If the string does not contain any cased characters or any of the cased characters are not uppercase then it returns False.

Example:

s = "THIS IS SPARTA!"
print(s.isupper())

Output:

True

In the above example, since all the cased characters are in uppercase the isupper() function returns True.

For more, refer to python string methods docs.

Other articles on python strings:


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top