0
0
Pythonprogramming~30 mins

Type conversion (int, float, string) in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Type conversion (int, float, string)
📖 Scenario: Imagine you are working in a small shop. You receive product prices as strings from the cashier's system, but you need to calculate totals and show prices properly formatted.
🎯 Goal: You will create variables with prices as strings, convert them to numbers to calculate totals, and then convert the result back to a string to display.
📋 What You'll Learn
Create variables with prices as strings
Create a variable to hold a discount rate as a float
Convert string prices to integers or floats for calculation
Calculate the total price after discount
Convert the total price back to a string for display
Print the final total price as a string
💡 Why This Matters
🌍 Real World
In shops or online stores, prices often come as text and need conversion to numbers for calculations like totals and discounts.
💼 Career
Understanding type conversion is essential for data processing, user input handling, and preparing data for calculations in many programming jobs.
Progress0 / 4 steps
1
Create price variables as strings
Create three variables called price1, price2, and price3 with the string values '100', '250.5', and '399' respectively.
Python
Need a hint?

Use quotes around numbers to make them strings, like price1 = '100'.

2
Create a discount rate as a float
Create a variable called discount_rate and set it to the float value 0.1 (which means 10%).
Python
Need a hint?

Write discount_rate = 0.1 without quotes to make it a float.

3
Convert prices to numbers and calculate total
Convert price1 and price3 to integers using int(), convert price2 to a float using float(), then calculate the total price after applying the discount rate. Store the result in a variable called total_price.
Python
Need a hint?

Use int() for whole numbers and float() for decimal numbers. Then add and apply discount.

4
Convert total to string and print
Convert the total_price to a string using str() and print it with the message: Total price after discount: followed by the converted string.
Python
Need a hint?

Use str(total_price) to convert the number to text, then print with the message.