How to Convert Lowercase Letters in String to Uppercase in Python?


In Python, a string is a sequence of characters. It's a type of data that represents text values, such as words, sentences, or even entire documents. Strings in Python are enclosed in either single quotes ('...') or double quotes ("..."), and can include alphanumeric characters, symbols, whitespace, and more.

We can perform various operations on strings, such as concatenation, slicing, and formatting. We can also use various built-in methods to manipulate and transform strings, such as converting them to uppercase or lowercase, splitting them into lists, and replacing substrings.

In this article we are considering some examples of converting lowercase letters in a string to uppercase in Python.

Using the upper() method

One way to convert lowercase letters to uppercase in a string is to use the upper() method.

Example

We are using the upper() method to convert all the lowercase letters in the string to uppercase. The upper() method returns a new string with all the alphabetic characters converted to uppercase.

# sample string
text = "hakuna matata"
# convert all lowercase letters to uppercase using the upper() method
text = text.upper()
print(text)

Output

HAKUNA MATATA

Using a loop to iterate over characters

We can also loop through each character in the string and convert any lowercase letters to uppercase using the upper() method.

Example

We are looping through each character in the string and checking if it's a lowercase letter using the islower() method. If it is, we convert it to uppercase using the upper() method and add it to the new_text variable. Otherwise, we just add the character to the new_text variable as is.

# sample string
text = "Welcome, aboard!"
# convert all lowercase letters to uppercase using a loop
new_text = ""
for char in text:
    if char.islower():
        new_text += char.upper()
    else:
        new_text += char
print(new_text)

Output

WELCOME, ABOARD!

Here are three more examples of how to convert lowercase letters in a string to uppercase in Python.

Using the capitalize() method

We can also use the capitalize() method to convert the first character of the string to uppercase, and leave the rest of the string as is.

Example

We are using the capitalize() method to convert only the first character of the string to uppercase. The rest of the string is left unchanged.

# sample string
text = "foo bar"
# convert the first character to uppercase using capitalize() method
text = text.capitalize()
print(text)

Output

Foo bar

Using the casefold() method

The casefold() method is similar to the lower() method, but it converts the string to lowercase in a way that's suitable for case-insensitive comparisons. We can then use the upper() method to convert the lowercase letters to uppercase.

Example

We are first using the casefold() method to convert the string to lowercase in a case-insensitive way. Then, we are using the upper() method to convert all the lowercase letters in the string to uppercase.

# sample string
text = "Hello, World!"
# convert all lowercase letters to uppercase using casefold() and upper() methods
text = text.casefold().upper()
print(text)

Output

HELLO, WORLD!

Using list comprehension

We can also use list comprehension to convert all lowercase letters in a string to uppercase.

Example

We are using list comprehension to loop through each character in the string and check if it's a lowercase letter using the islower() method. If it is, we convert it to uppercase using the upper() method. Otherwise, we just add the character to a list as is. We then join the list back into a string using the join() method.

# sample string

text = "Fubar, baz!"
# convert all lowercase letters to uppercase using list comprehension
text = ''.join([char.upper() if char.islower() else char for char in text])
print(text)

Output

FUBAR, BAZ!

Updated on: 10-Aug-2023

689 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements