DEV Community

Cover image for How to Uppercase Strings in Python
Jonathan Kiptoon
Jonathan Kiptoon

Posted on

How to Uppercase Strings in Python

Python has a variety of ways to convert strings to uppercase. The simplest is to use the upper() method on a string. For example:

name = "Ada Lovelace"
uppercase_name = name.upper() 
print(uppercase_name)
# Prints "ADA LOVELACE"
Enter fullscreen mode Exit fullscreen mode

The upper() method returns a new string with all alphabetic characters converted to uppercase. It does not modify the original string.

You can also use upper() to convert parts of a string to uppercase. For example:

title = "ada lovelace: the first computer programmer"
title_uppercase = title.upper()
print(title_uppercase)
# Prints "ADA LOVELACE: THE FIRST COMPUTER PROGRAMMER"
Enter fullscreen mode Exit fullscreen mode

This converts the entire string to uppercase. To only convert part of the string, slice the string and call upper() on the slice:

title = "ada lovelace: the first computer programmer"
title_slice = title[:12].upper()
print(title_slice)  
# Prints "ADA LOVELACE: the first computer programmer"
Enter fullscreen mode Exit fullscreen mode

The slice [:12] extracts the first 12 characters, uppercases them, and leaves the rest of the string intact.

You can also use the str.capitalize() method to uppercase just the first character of a string:

title = "ada lovelace: the first computer programmer"
print(title.capitalize())
# Prints "Ada lovelace: the first computer programmer"
Enter fullscreen mode Exit fullscreen mode

This is useful for capitalizing proper nouns and sentence starts.

For more flexible case conversion, use str.casefold(), str.lower() and str.swapcase():

  • str.casefold() - Convert string to lowercase, more aggressive than lower()
  • str.lower() - Convert string to lowercase
  • str.swapcase() - Flip cases of all letters in string

For example:

name = "Ada Lovelace" 

lower_name = name.lower() # "ada lovelace"
cf_name = name.casefold() # "ada lovelace" 

yell_name = name.upper() # "ADA LOVELACE"
scream_name = yell_name.swapcase() # "ada lovelace"
Enter fullscreen mode Exit fullscreen mode

casefold() is more aggressive than lower() at converting characters to lowercase equivalents. lower() only converts ASCII letters while casefold() converts more Unicode characters.

The str.title() method capitalizes the first letter of every word in a string:

book_title = "the hitchhiker's guide to the galaxy"
print(book_title.title()) 
# Prints "The Hitchhiker's Guide To The Galaxy"
Enter fullscreen mode Exit fullscreen mode

This is useful for generating title case strings.

For more advanced case operations, import the string module:

import string

s = "Ada Lovelace is cool!"

print(string.capwords(s)) 
# Prints "Ada Lovelace Is Cool!" 

print(string.capwords(s, sep=None))
# Prints "Ada Lovelace is cool!"
Enter fullscreen mode Exit fullscreen mode

string.capwords() capitalizes the first letter of every word and lowercases other letters. It takes an optional separator argument to specify word boundaries.

The string module also provides constants for ascii uppercase, lowercase and punctuation:

print(string.ascii_uppercase)
# Prints "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

print(string.ascii_lowercase)
# Prints "abcdefghijklmnopqrstuvwxyz" 

print(string.punctuation)
# Prints "!'(),-.:;?[]_{}"
Enter fullscreen mode Exit fullscreen mode

These provide convenient access to ASCII character sets for case and punctuation manipulation.

Some other helpful string case methods:

  • str.istitle() - Check if string is titlecased
  • str.islower() - Check if string is all lowercase
  • str.isupper() - Check if string is all uppercase

For example:

title = "The Hitchhiker's Guide To The Galaxy"

print(title.istitle()) # True
print(title.islower()) # False
print(title.isupper()) # False
Enter fullscreen mode Exit fullscreen mode

These methods test the case conventions of the string and return a boolean.

To summarize, Python has great built-in string methods for uppercase conversion:

  • upper() - Convert string to uppercase
  • capitalize() - Capitalize first letter
  • casefold() - Aggressive lowercase conversion
  • lower() - Convert string to lowercase
  • swapcase() - Flip cases of all letters
  • title() - Capitalize first letter of each word

The string module provides additional case utilities like capwords() and case constants.

And methods like istitle(), islower(), isupper() test case conventions.

By using these tools, you can easily handle all your Python string uppercasing needs. Correct casing can go a long way towards making strings more readable and usable.

Top comments (0)