Case Conversion Methods upper() and lower()
How can you convert the string "apple"
to "APPLE"
, or "APPLE"
to "apple"
in one go?
Python provides methods upper()
and lower()
to convert the case of characters in strings.
Using Case Conversion Methods
The upper()
method converts all characters in a string to uppercase, while the lower()
method converts them to lowercase.
text = "Hello World!" upper_text = text.upper() # HELLO WORLD! print("upper_text:", upper_text) lower_text = text.lower() # hello world! print("lower_text:", lower_text)
When is Case Conversion Needed?
In programming, case conversion is mainly used during normalization
, where user input is transformed into a standard format.
What is Normalization? : Normalization refers to the process of converting data into a consistent format. For example, transforming all text to lowercase or removing spaces for comparison purposes are typical normalization tasks.
user_input = "PyThOn" standardized_input = user_input.lower() if standardized_input == "python": print("Matched!") else: print("Not Matched.")
What is the function in Python to convert all characters of a string to uppercase?
capitalize()
title()
upper()
lower()
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result