SypherTec
…
PART 1 · 05 OF 074 MIN READUPDATED 20 NOV 2026

Types and Conversion: Text That Looks Like a Number

Your tracker adds amounts you typed as numbers. This lesson adds one that arrives as text, the way a total copied from a receipt would. Your program converts it before adding it.

The concept

Here is your tracker as the last lesson left it. Run it:

WHERE YOU LEFT OFF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Now run this. The digits are the same on both lines:

SAME DIGITS, TWO RESULTS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

The first line printed 2500300. The second printed 2800. With quotes, the values are strings, and + joins strings end to end. Without quotes, they are numbers, and + adds them.

So Python is always tracking what kind of value it holds. That kind is called the value's type. You can ask Python for it:

ASKING PYTHON WHAT TYPE A VALUE IS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

int is a whole number. float is a number with a decimal point. str is Python's short name for a string. type() is a built-in function that reports which one a value is.

What happens when the two kinds meet? Run this:

TEXT PLUS A NUMBER · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
TypeError: can only concatenate str (not "int") to str

Concatenate means join text end to end. Python sees a string on the left, so it expects to join another string onto it. It finds an int instead and stops. Python will not guess whether you meant "add" or "join". You have to say.

Why would an amount be text at all? Values that come from outside your program usually arrive as text: something a person typed, a line in a file, a field on a website. In this lesson, quotes stand in for that. The fix is the same wherever the text came from. Convert it:

TEXT CONVERTED TO A NUMBER · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

int(receipt_total) read the text "4200" and gave back the number 4200. The program stored that number in fuel, and fuel + 300 added up to 4500. The last line confirms it: fuel is an int.

The syntax

fuel = int("4200")
price = float("2500.50")
label = str(4200)
  • int(...) converts to a whole number.
  • float(...) converts to a number with a decimal point.
  • str(...) converts to text.

Each one gives back a new value. It does not change the value you passed in, so store the result in a variable.

int accepts text made of digits, with or without spaces around them: int(" 4200 ") is 4200. It refuses anything else, including commas and decimal points.

A minimal working example

main.py
receipt_total = "4200"
fuel = int(receipt_total)
total = 1500 + fuel
print(f"Total spent: ₦{total:,}")

The amount arrives as text, gets converted once, and from then on it is an ordinary number. Output: Total spent: ₦5,700.

Common mistakes

You add text and a number. Depending on which side the text is on, the message changes. "2500" + 300 gives TypeError: can only concatenate str (not "int") to str. 300 + "2500" gives TypeError: unsupported operand type(s) for +: 'int' and 'str'. The wording differs. The cause is the same: a str and an int on either side of +. Find the value that is still text and convert it.

You convert but never store the result. This trips up almost everyone. Run this:

A CONVERSION THAT IS THROWN AWAY · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

The TypeError points at line 3, even though line 2 converted fuel. int(fuel) made a number, and then nothing kept it. fuel still holds the text "4200". Write fuel = int(fuel) so the variable holds the number.

The text has a comma in it. Run this:

TEXT WITH A COMMA IN IT · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
ValueError: invalid literal for int() with base 10: '4,200'

"Invalid literal" means the text is not written the way a whole number is written in code. "Base 10" is ordinary counting, and you can ignore it. Text like "2500.50" fails the same way: use float for that. Text with a comma cannot be converted as it is. Part 3 of this track shows how your program can skip a line like this instead of stopping.

Try it yourself

A fourth expense arrives as text: a fuel receipt. You will see it fail, then convert it.

  1. Run the starting code below. Write down your total.
  2. Under the data_bundle line, add fuel_receipt = "4200", with the quotes. Then add print(type(fuel_receipt)) as the very last line of the file. Run it. Confirm the last line of output is <class 'str'>.
  3. Break it on purpose: add + fuel_receipt to the end of your total line. Run it. Confirm the console shows TypeError: unsupported operand type(s) for +: 'int' and 'str', pointing at your total line. Nothing printed, because the total line runs before every print.
  4. Fix it: under fuel_receipt, add fuel = int(fuel_receipt). In your total line, change fuel_receipt to fuel. Change the last line to print(type(fuel)). Run it. Confirm your total is 4,200 more than the one you wrote down, and the last line is <class 'int'>.
  5. Delete the print(type(fuel)) line. It was only there for checking. Run it. Confirm your report ends with the total again.
TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Your tracker can take an amount that arrived as text. It also has four separate expense names now. Every new expense means another name and a longer total line. The next lesson keeps all your expenses under one name.

QUIZ · 3 QUESTIONSCheck what stuckStart the quiz
PART 1 · 05 OF 07❮❯