Case Conversion Methods upper() and lower()
How can you convert the string "apple"
to "APPLE"
or "APPLE"
to "apple"
in one step?
Python provides the upper()
and lower()
methods to change the case of characters in a string.
Using Case Conversion Methods
The upper()
method converts all characters in a string to uppercase, and the lower()
method converts them to lowercase.
Case Conversion Example
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 consistent, standard format.
What is Normalization? : Normalization is the process of converting data into a consistent format. Common tasks include transforming all text to lowercase or removing spaces to facilitate comparison.
Data Normalization Example
user_input = "PyThOn" standardized_input = user_input.lower() if standardized_input == "python": print("Matched!") else: print("Not Matched.")
Mission
0 / 1
What function in Python converts all characters of a string to uppercase?
capitalize()
title()
upper()
lower()
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Run
Generate
Execution Result