GCSE · Computer Science · AQA · Spec 8525

String to integer / string to real conversion

Type 42 into a program and it doesn't receive the number forty-two. It receives 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. On which line 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

Use Next to step through. Before each step, predict 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 the same 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

What if the program skipped the conversion step? Both variables here 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"

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 which conversion to choose.

What you need to know

  • Digits typed by a user are stored as a string until the program converts them.
  • String-to-integer turns whole-number text into an integer: int("42") gives 42.
  • String-to-real turns number text into a real: float("2.75") gives 2.75, and float("42") gives 42.0.
  • Text with a decimal point, even "3.0", converts to a real but not to an integer.

The big picture

Anything a user types into a program arrives as a string, even if it is all digits. Before the program can do arithmetic with it, it must convert it: string-to-integer (int() in Python) for whole-number text, and string-to-real (float() in Python) for text that may contain a decimal point. Whole-number text converts both ways, text with a decimal point converts only to a real, and text that isn't a number written in digits converts to neither.

Key points

1input() always returns a string, whatever the user types.
2Convert first, calculate second: arithmetic must use the converted variable, not the original string.
3Pick the conversion from the kind of value: counts and ages suit string-to-integer; prices, heights and temperatures need string-to-real.
4Text that isn't a number written in digits, such as "twelve" or "1,000", 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) joins the text first to make "64" and gives 64, not 10. Convert each value first: int(a) + int(b) gives 10.

🧠

Memory hook

Quotes mean text. "4" is a character; 4 is a number. int() is for whole-number text, float() is for anything with a point, 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?
A string, even when the user types only digits.
What does string-to-integer do?
It turns whole-number text into an integer, e.g. int("42") gives 42.
What does string-to-real do?
It turns number text into a real number, e.g. float("2.75") gives 2.75. Python calls a real a float.
What does "12" + "30" give, and why?
"1230". Both are strings, so + joins the characters instead of adding numbers.
Can int() convert the string "3.0"?
No. The text contains a decimal point, so int() fails. float("3.0") gives 3.0.
What does float("42") give?
The real number 42.0. Whole-number text converts to a real as well as to an integer.
What happens if you try to convert "twelve" or "1,000"?
Neither conversion accepts it, because it isn't a number written only in digits, so the program stops with an error.
How do you choose between string-to-integer and string-to-real?
Use string-to-integer for whole-number values such as counts and ages; use string-to-real for values that can have a decimal part, such as prices and heights.

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 29 September 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.