GCSE · Computer Science · AQA · Spec 8525

String to integer / string to real conversion

Type 42 into a program and it doesn't get forty-two. It gets two characters, a 4 and a 2. So how do they become a number?

Watch the type column

Same digits, new data type

A ticket program in Python. The user types 4 for the number of tickets and 2.75 for the price of one. Step through and watch what the program actually stores.

Keep your eye on the type column. At which step does the 4 stop being text?

1 tickets_text = input("How many tickets? ")
2 price_text = input("Price of one ticket? ")
3 tickets = int(tickets_text)
4 price = float(price_text)
5 total = tickets * price
6 print("Total:", total)
variablevaluetype
tickets_text"4"string

Output

 

Step 1: The user types 4. But input() always hands back text, so tickets_text holds the string "4": one character, not the number four. The quotes are your clue.

1 / 6

Press Next to step through. Before each step, guess the value and the type, then check.

Step 1 of 6: The user types 4. But input() always hands back text, so tickets_text holds the string "4": one character, not the number four. The quotes are your clue..

Exam line: Convert first, calculate second. The line that converts a value must run before any line that does arithmetic with it.
Watch out: "4" and 4 look almost identical on screen, but the first is a one-character string and the second is an integer. Only the integer is a number the program can calculate with.

Predict, then check

Nothing has been converted here. Both variables hold text straight from input().

a = input() and the user types 12. Then b = input() and the user types 30. What does print(a + b) display?

Which text can each conversion read?

Sort the strings

For each string, tick every Python conversion that would accept it. Some go in both circles, some in just one, and some in neither.

  • A int() accepts it
  • B float() accepts it
  1. "42"
  2. "-7"
  3. "3.5"
  4. "3.0"
  5. "twelve"
  6. "1,000"
  7. "4 kg"

Your turn

Write the conversions yourself

A program asks for a user's age in whole years and their height in metres. input() gives back both as strings, stored in age_text and height_text (for example "15" and "1.75"). Write Python code that converts each value to a suitable data type, then outputs the user's age in 10 years' time and their height in centimetres. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Digits typed into a program are just characters until you convert them. Here's how, and how to pick the right conversion.

What you need to know

  • Anything typed into a program arrives as a string, even when it's all digits: typing 42 gives the text "42".
  • Digits held as text aren't a number yet, so the program must convert them before doing any arithmetic.
  • Have a goPriya types her age, 15, and announces the program can now add 1 to it for next year's birthday. Straight after input(), is she right?

    Not yet. It's still the text "15".

    Digits from input() stay a string until something converts them, however much they look like a number, so there's nothing to add 1 to yet.

  • String-to-integer turns whole-number text into an integer. Python writes it int(): int("42") gives 42.
  • String-to-real turns number text into a real. Python calls a real a float: float("2.75") gives 2.75.
  • Whole-number text works both ways, but string-to-real always gives a real: float("42") gives 42.0.
  • Have a goCommit before you peek: does float("7") give 7, 7.0 or an error?

    7.0

    String-to-real happily takes whole-number text, but it always hands back a real, so you get 7.0. Plain 7 would be an integer, which is int()'s job.

  • A decimal point blocks string-to-integer, even in "3.0". int() doesn't round or chop the text; it fails.
  • A minus sign is fine for both conversions: int("-7") gives -7 and float("-7") gives -7.0.
  • Have a goWhich of these would int() accept: "-12", "12.0" or "12.5"?

    Only "-12".

    A minus sign is fine, but any decimal point stops int(), even when the value after it is zero.

  • A number word, a comma separator or a unit on the end, as in "twelve", "1,000" or "4 kg", blocks both conversions.
  • Choose by the kind of value: counts and ages suit integers; prices, heights and temperatures need reals.
  • Convert first, then calculate with the converted variable, never with the original string.

The big picture

Whatever a user types arrives in the program as a string, even when it's all digits. Before the program can calculate with it, it has to convert it. String-to-integer (int() in Python) turns whole-number text into an integer. String-to-real (float() in Python) turns number text into a real, and always gives a real, so "42" becomes 42.0. Text with a decimal point, even "3.0", converts to a real but not to an integer. Text like "twelve", "1,000" or "4 kg" converts to neither, and the program stops with an error. Convert first, then calculate with the converted values.

Key points

1input() always hands back a string, whatever the user types.
2Python spells the two operations int() for string-to-integer and float() for string-to-real.
3Convert first, calculate second: the arithmetic must use the converted variable, not the original string.
4Pick the conversion from the kind of value: counts and ages suit string-to-integer; prices, heights and temperatures need string-to-real.
5Text such as "twelve", "1,000" or "4 kg" can't be converted, and the program stops with an error.

Worked example

Problem

A weather program reads two temperatures typed by the user: morning_text holds "18.5" and afternoon_text holds "21". How should the program work out how much warmer the afternoon was?

⚠ Watch out

Doing the arithmetic before converting. If a holds "6" and b holds "4", then int(a + b) glues the text first to make "64", and gives 64, not 10. Convert each value on its own first: int(a) + int(b) gives 10.

🧠

Memory hook

Quotes mean text: "4" is a character, 4 is a number. int() wants whole-number text, float() takes a point too, and neither reads words.

✓

Check yourself

Cover the page. For each string, say which conversion would accept it (int(), float(), both or neither) and what each successful conversion gives: "250", "0.5", "5.0", "five".

Flashcards

(8)
What data type does input() give back in Python?
A string, even when the user types nothing but digits.
What does string-to-integer do?
It turns whole-number text into an integer. In Python: int("42") gives 42.
What does string-to-real do?
It turns number text into a real. In Python it's float(), because Python calls a real a float: float("2.75") gives 2.75.
What does "12" + "30" give, and why?
"1230". Both are strings, so + glues the characters together instead of adding numbers.
Can int() convert the string "3.0"?
No. The text has a decimal point, so int() fails, even though the value is whole. float("3.0") works and gives 3.0.
Which conversions accept whole-number text such as "42", and what does each give?
Both. int("42") gives the integer 42; float("42") gives the real 42.0.
What happens if you try to convert "twelve", "1,000" or "4 kg"?
Neither conversion accepts them: a number written as a word, a comma separator or a unit on the end isn't number text. The program stops with an error.
How do you choose between string-to-integer and string-to-real?
Look at the kind of value. Whole-number values such as counts and ages suit string-to-integer; values that can have a decimal part, such as prices and heights, need string-to-real.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 26 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.