Exercise 2-4 Solution – Python Crash Course Chapter 2: Variables and Simple Data Types

 

πŸ“˜ 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:

  1. Store a name in a variable:

    The variable name is assigned the string "toNy StArk", which represents a person's name.

  2. Print the original name:

    The print() function displays the original value of the name variable.

  3. 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.

  4. 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.

  5. 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:


“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