Case Conversion Methods upper() and lower()
How can you convert the string "apple"
to "APPLE"
, or "APPLE"
to "apple"
in one step?
Python provides methods upper()
and lower()
to change the case of characters in a string.
Using Case Conversion Methods
The upper()
method converts all characters in a string to uppercase, while 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.")
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.