Module 1 ยท Lesson 4 of 5

Type Conversion & f-Strings

Now that you know the four core types, it's time to learn how to transform data between them โ€” and how to build beautiful formatted output with Python's most modern string tool.

โœ“ Convert between types with int(), str(), float()
โœ“ Format strings with f-string syntax
โœ“ Understand when and why to cast types
โœ“ Explore truthiness with bool()

Type Conversion (Casting)

Python gives you built-in functions to convert values from one type to another. This is called casting.

casting.py
# String to Integer
age_text = "25"
age = int(age_text)
print(age + 5)            # โ†’ 30  (now it's a number!)

# Integer to String
score = 100
print("Score: " + str(score))  # โ†’ Score: 100

# String to Float
price = float("9.99")
print(price * 2)            # โ†’ 19.98

# Float to Integer (truncates โ€” doesn't round!)
print(int(3.9))              # โ†’ 3  (decimal just gets chopped)
print(int(3.1))              # โ†’ 3
โš ๏ธ Not everything converts

int("hello") will crash with a ValueError โ€” Python can only convert strings that actually look like numbers. Always make sure the data is valid before casting.

f-Strings: The Modern Way

Concatenation with + works, but it gets messy fast. Python's f-strings (formatted string literals) are cleaner, more readable, and don't require manual type conversion.

fstrings.py
name = "Kira"
level = 7
hp = 85.5

# โŒ Old way โ€” messy concatenation
print("Player: " + name + " | Level: " + str(level) + " | HP: " + str(hp))

# โœ… f-string โ€” clean and readable
print(f"Player: {name} | Level: {level} | HP: {hp}")

# Both output: Player: Kira | Level: 7 | HP: 85.5

# You can even put expressions inside the braces!
print(f"Next level: {level + 1}")     # โ†’ Next level: 8
print(f"Name length: {len(name)}")   # โ†’ Name length: 4
๐Ÿ’ก f-String Recipe

Just add f before the opening quote, then put any variable or expression inside {curly braces}. Python handles the type conversion for you automatically!

๐ŸŽฎ

Challenge 1: Build a Player Card

Easy

Use an f-string to create a formatted player stat card from the given variables.

Instructions: Create a variable called card using an f-string that produces this exact output:

โš”๏ธ Warrior Aria | LVL 12 | HP 95.5
Start with f" then build the string: f"โš”๏ธ {player_class} {name} | LVL {level} | HP {hp}". The variables inside the curly braces get replaced with their values automatically.
๐Ÿงพ

Challenge 2: Receipt Printer

Easy

A simple store receipt. The prices come in as strings (as they would from user input). Convert them to numbers, calculate the total, and print a formatted receipt.

Instructions:
โ€ข Convert price_a and price_b from strings to floats
โ€ข Calculate total by adding them together
โ€ข Print the total as: Total: $XX.XX (use an f-string)
First convert each price: total = float(price_a) + float(price_b). The total should be 21.25. Your f-string for the total line is already provided โ€” just make sure total has the right value.
๐Ÿชช

Challenge 3: User Profile

Medium

Build a multi-line profile card using f-strings and the given data. This tests your ability to combine multiple f-strings with different data types.

Instructions: Print exactly this output using f-strings (use the variables, don't hardcode the values):

โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
Name: Nova
Role: Pilot
Missions: 47
Rating: 4.8/5.0
Active: True
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Between the top and bottom borders, add 5 print statements with f-strings: print(f" Name: {username}"), print(f" Role: {role}"), etc. Notice the two spaces before each label. For rating, use: f" Rating: {rating}/5.0"
๐Ÿ”ฎ

Challenge 4: Truthiness

Stretch

Python's bool() function can convert any value into True or False. Some surprising things are "truthy" and some are "falsy." Explore and discover the pattern!

Instructions: Predict what bool() returns for each value. Set each answer to True or False (actual booleans, no quotes). Look for the pattern!
Here's the pattern: "empty" things are False, everything else is True. So: 0, 0.0, "" (empty string), and None are all falsy. But "0" is a non-empty string (it contains the character zero), so it's True. Same with "False" โ€” it's a non-empty string!