
PYTHON — Change Case Solution in Python
Computer science is no more about computers than astronomy is about telescopes. — Edsger W. Dijkstra
In this tutorial, you’ll learn how to change the case of strings in Python. We’ll cover converting strings to lowercase and uppercase, using both manual and automated methods.
Let’s start by defining four strings and assigning them to variables:
string1 = "Animals"
string2 = "Badger"
string3 = "Honey Bee"
string4 = "Honey Badger"To convert each string to lowercase and print them on separate lines, you can use the lower() method:
print(string1.lower())
print(string2.lower())
print(string3.lower())
print(string4.lower())Similarly, to convert the strings to uppercase and print them on separate lines, you can use the upper() method:
print(string1.upper())
print(string2.upper())
print(string3.upper())
print(string4.upper())If you want to automate this process, you can use a list to store the strings and create functions to loop over the list and apply the case conversion. Here’s an example using functions to convert the strings to lowercase and uppercase:
list_A = ["Animals", "Badger", "Honey Bee", "Honey Badger"]
def convert_to_lowercase(strings):
for string in strings:
print(string.lower())
def convert_to_uppercase(strings):
for string in strings:
print(string.upper())
convert_to_lowercase(list_A)
convert_to_uppercase(list_A)When you run the code, you’ll see the output with the strings converted to lowercase and uppercase, each on a separate line.
By using the lower() and upper() string methods, you can easily change the case of strings in Python. This can be useful for tasks such as data normalization, text processing, and formatting output. As you continue to learn Python, you'll discover more efficient ways to manipulate strings and automate repetitive tasks.






