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 * 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.
Use Next to step through. Before each step, predict the value and the type, then check.
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?
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
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?
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"?
What does float("42") give?
What happens if you try to convert "twelve" or "1,000"?
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 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.