π Python Crash Course – Chapter 2: Variables and Simple Data Types – Exercise 2-4 Solution
π Exercise 2-4
Task: Store a person’s name in a variable, and then print that person’s name in lowercase, uppercase, and titlecase .
✅ Solution Code:
name= "toNy StArk"
print(f"Original name : {name}")
print(f"Lowercase name : {name.lower()}")
print(f"Uppercase name : {name.upper()}")
print(f"Title name : {name.title()}")
π§ Code Explanation:
-
Store a name in a variable:
The variable
name
is assigned the string"toNy StArk"
, which represents a person's name. -
Print the original name:
The
print()
function displays the original value of thename
variable. -
Convert and print the name in lowercase:
The
lower()
method is used to convert all characters in the name to lowercase, and the result is printed. -
Convert and print the name in uppercase:
The
upper()
method is used to convert all characters in the name to uppercase, and the result is printed. -
Convert and print the name in titlecase:
The
title()
method is used to capitalize the first letter of each word in the name, and the result is printed.
π Output:
Original name : toNy StArk
Lowercase name : tony stark
Uppercase name : TONY STARK
Title name : Tony Stark
π Related Exercises from Chapter 2:
- ➤ Exercise 2-1 | Simple Message Part 1
- ➤ Exercise 2-2 | Simple Messages Part 2
- ➤ Exercise 2-3 | Personal Message
- ➤ Exercise 2-4 | Name Cases
- ➤ Exercise 2-5 | Famous Quote
- ➤ Exercise 2-6 | Famous Quote 2
- ➤ Exercise 2-7 | Stripping Names
- ➤ Exercise 2-8 | Number Eight
- ➤ Exercise 2-9 | Favorite Number
- ➤ Exercise 2-10 | Adding Comments
- ➤ Exercise 2-11 | Zen of Python
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment