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 * price6 print("Total:", total)
| variable | value | type |
|---|---|---|
| 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.
Press Next to step through. Before each step, guess the value and the type, then check.
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?
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
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?
What does string-to-integer do?
What does string-to-real do?
What does "12" + "30" give, and why?
Can int() convert the string "3.0"?
Which conversions accept whole-number text such as "42", and what does each give?
What happens if you try to convert "twelve", "1,000" or "4 kg"?
How do you choose between string-to-integer and 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 postedMore AQA GCSE Computer Science topics
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.