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.
int(), str(), float()bool()Type Conversion (Casting)
Python gives you built-in functions to convert values from one type to another. This is called casting.
# 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
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.
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
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
Use an f-string to create a formatted player stat card from the given variables.
โ๏ธ Warrior Aria | LVL 12 | HP 95.5
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
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.
โข 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)
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
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.
โโโโโโโโโโโโโโโโโโโโ
Name: Nova
Role: Pilot
Missions: 47
Rating: 4.8/5.0
Active: True
โโโโโโโโโโโโโโโโโโโโ
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
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!
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!