Python Language String Methods Changing the capitalization of a string

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


Python's string type provides many functions that act on the capitalization of a string. These include :

  • str.casefold
  • str.upper
  • str.lower
  • str.capitalize
  • str.title
  • str.swapcase

With unicode strings (the default in Python 3), these operations are not 1:1 mappings or reversible. Most of these operations are intended for display purposes, rather than normalization.

Python 3.x3.3


str.casefold creates a lowercase string that is suitable for case insensitive comparisons. This is more aggressive than str.lower and may modify strings that are already in lowercase or cause strings to grow in length, and is not intended for display purposes.

# 'xssσ'

# 'xßς'

The transformations that take place under casefolding are defined by the Unicode Consortium in the CaseFolding.txt file on their website.


str.upper takes every character in a string and converts it to its uppercase equivalent, for example:

"This is a 'string'.".upper()


str.lower does the opposite; it takes every character in a string and converts it to its lowercase equivalent:

"This IS a 'string'.".lower()
# "this is a 'string'."


str.capitalize returns a capitalized version of the string, that is, it makes the first character have upper case and the rest lower:

"this Is A 'String'.".capitalize() # Capitalizes the first character and lowercases all others
# "This is a 'string'."


str.title returns the title cased version of the string, that is, every letter in the beginning of a word is made upper case and all others are made lower case:

"this Is a 'String'".title()
# "This Is A 'String'"


str.swapcase returns a new string object in which all lower case characters are swapped to upper case and all upper case characters to lower:

"this iS A STRiNG".swapcase() #Swaps case of each character
# "THIS Is a strIng"

Usage as str class methods

It is worth noting that these methods may be called either on string objects (as shown above) or as a class method of the str class (with an explicit call to str.upper, etc.)

str.upper("This is a 'string'")

This is most useful when applying one of these methods to many strings at once in say, a map function.

# ['THESE', 'ARE', 'SOME', "'STRINGS'"]

Got any Python Language Question?