5 most useful string casing methods in Python (17/100 Days of Python)

Martin Mirakyan
2 min readJan 18, 2023

--

Day 17 of the “100 Days of Python” blog post series covering string casing methods

Python offers a variety of built-in methods for manipulating the case of strings. These methods can be useful for formatting text, parsing user input, and more. In this tutorial, we’ll take a look at 5 of the most useful string casing methods in Python:

1.str.upper(): This method converts all of the characters in a string to uppercase (if they are alphabetical):

string = 'Hello World'
print(string.upper()) # HELLO WORLD
print('Hey'.upper()) # HEY
print('Agent 007!'.upper()) # AGENT 007!

2. str.lower(): This method converts all of the characters in a string to lowercase:

string = 'Hello World'
print(string.lower()) # hello world
print('Hey'.lower()) # hey
print('Agent 007!'.lower()) # agent 007!

3. str.title(): This method capitalizes the first letter of each word in a string:

string = 'hello world'
print(string.title()) # Hello World
print('hey'.title()) # Hey
print('Agent 007!'.title()) # Agent 007!

4. str.capitalize(): This method capitalizes the first letter of the first word in a string:

string = 'hello world'
print(string.capitalize()) # Hello world
print('hey'.capitalize()) # Hey
print('Agent 007!'.capitalize()) # Agent 007!
print('hello. how are you? Good?'.capitalize())
# Hello. how are you? good?

5. str.swapcase(): This method inverts the case of all the letters in a string:

string = 'hello world'
print(string.swapcase()) # HELLO WORLD
print('Hey'.swapcase()) # hEY
print('Agent 007!'.swapcase()) # aGENT 007!
print('Hello. how are you? Good?'.swapcase())
# hELLO. HOW ARE YOU? gOOD?

So, Python offers a variety of built-in methods for manipulating the case of strings. The above-mentioned methods are the most commonly used ones, but there are many more string casing methods.

What’s next?

--

--