Turn text into numbers and numbers into text with int(), float(), str() and bool(), and understand why some conversions fail.
About 35 minBeginner4 exercisesFree
You will be able to
Check the type of any value with type()
Convert between str, int and float with int(), float() and str()
Predict what int() does to a float and to text containing a decimal point
Explain why some conversions raise ValueError or TypeError
Avoid the bool("False") trap when turning text into a yes-or-no value
Data rarely arrives in the type you need. A quantity typed by a user is text. A price read from a spreadsheet export is text. A reference number like 0042looks like a number but must stay text, or its leading zeros disappear. Knowing how to move a value from one type to another — and when not to — is what stops programs from printing 33333 instead of 15.
Every value has a type. You've used four so far: int (whole numbers), float (numbers with a decimal point), str (text) and bool (True/False). The built-in function type() tells you which one you have.
int() and float() only accept text that actually spells a number. Anything else raises a ValueError — the value has the right type (a string) but the wrong content:
int("twelve") — not digits
int("12.5") — int() does not accept a decimal point in text (convert with float() first, then int())
int("") — empty text, for example when someone presses Enter without typing
float("12,5") — Python uses a point, not a comma, for decimals
Try it: Change the last line to print(int(float(weight))) so it works. What does it print?
Read the last line of the error message: ValueError: invalid literal for int() with base 10: '12.5'. It names the function (int()) and shows the exact text that couldn't be converted, quotes included — which also reveals stray spaces or empty strings.
Text that must stay text
Not every string of digits is a number. Phone numbers, postcodes, order references and product codes are identifiers: you never do arithmetic on them, and converting them loses information. int("0042") is 42 — the leading zeros are gone. Keep identifiers as strings.
Guided practice · ●difficulty 1 of 5 · about 4 min
Calculate a line total
A till reads two lines: the quantity (a whole number) and the unit price (may have decimals). Use input() with no prompt text — the tests type the values for you.
Store the quantity as an int in qty, the price as a float in price, multiply them into total, and print the total with two decimal places:
Total: 37.50
That is the output for quantity 3 and price 12.5.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Challenge · ●●difficulty 2 of 5 · about 7 min
Print a parcel label
A courier prints a label for each parcel. The program reads three lines (input() with no prompt text):
the parcel reference, such as 0042 — an identifier, not a quantity,
the weight in grams, a whole number such as 1250,
the rate per kilogram in SAR, such as 15.
Store the weight in kilograms in kg (grams divided by 1000) and the delivery cost in cost (kg times rate). Print one line:
Parcel 0042: 1.25 kg, cost 18.75 SAR
Show kg and cost with two decimal places. The reference must appear exactly as typed.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Edge cases · ●●●difficulty 3 of 5 · about 6 min
Box counts from a spreadsheet
A stock spreadsheet exports box counts as text with a decimal point, such as "12.0", and sometimes a partly used box appears as "7.9". Read one line (input() with no prompt text) and store the number of whole boxes as an int in boxes. Drop any fraction — a partly used box doesn't count, so 7.9 becomes 7, not 8.
Print:
Whole boxes: 12
The exported text may also have spaces around it, like " 30.0 ".
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Debugging · ●●difficulty 2 of 5 · about 5 min
Fix the newsletter opt-in
A sign-up form asks "Subscribe to the newsletter? (yes/no)" and the program stores the answer as a boolean in subscribed. The marketing team says everyone ends up subscribed — even people who typed no.
Fix the program so that subscribed is True only when the answer is yes, ignoring capital letters and spaces around it (" YES " counts as yes). Any other answer, including an empty one, means False. Keep the output format:
Subscribed: False
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Summary
type() tells you a value's type; int(), float(), str() and bool() build a new value of that type.
Conversion returns a new value: assign it to keep it.
int() truncates floats and rejects text with a decimal point; convert via float() when needed.
ValueError means the text doesn't spell a number; TypeError means the operation doesn't suit the type.
Keep identifiers like references and phone numbers as strings, and compare text instead of using bool() for yes/no answers.