GCSE · Computer Science · AQA · Spec 8525

Integer / real to string conversion

In Python, "Score: " + 2048 stops your program dead. The number has to become text first — and exactly when you convert it changes the answer.

Python · watch the types

Same digits, new job

Step through the program. Next to every value is its data type — keep your eye on that, not just the digits.

Before you press Next: the digits of score never change in this program. So what does?

1 score = 2048
2 time = 12.5
3 score_text = str(score)
4 time_text = str(time)
5 message = "Score " + score_text + " in " + time_text + " seconds"
6 print(message)
7 digits = len(score_text)
8 print(digits)

Variables

score=2048 · integer

Output

 

Step 1: score holds a whole number, so its type is integer. It can do maths: score + 1 would give 2049.

1 / 8

Press Next and watch the type after each value. The digits stay put; the type is what changes.

Step 1 of 8: score holds a whole number, so its type is integer. It can do maths: score + 1 would give 2049..

Watch out: Look at the quote marks. 2048 is a number; "2048" is text made of the characters 2, 0, 4 and 8. They look almost the same on screen, but a program treats them completely differently.

Predict, then check

Two lines, same numbers, same str(). The only difference is the moment the conversion happens.

Line A, print("Total: " + str(3 + 4)), prints Total: 7. What does line B, print("Total: " + str(3) + str(4)), print?

Your call

Convert it, or leave it?

Put each job in the right column.

Still to sort

Convert to a string first (0)

The number has to behave like text: joined into a message, or treated as characters.

Where the line is: It's the JOB that decides, not how the number looks. Text jobs need a string.

Use the number as it is (0)

The job is maths, or comparing sizes.

Where the line is: Arithmetic and comparisons need a number. Turning it into text first would stop them working.

6 of 6 still to sort.

Each card is a job a program needs to do with a number. Decide before you read why.

Spot the bug

Why did the program stop?

This Python program should print three lines about a run. It prints Laps run: 6 and then stops with a TypeError. Which line is to blame?

A program with one bug — which line goes wrong?

Your turn to write it

Write it, then check it

Answered 12 questions, average 4.5 seconds

A quiz app stores how many questions a player answered in the integer variable answered, and their average time per question, in seconds, in the real variable avg_time. Write code (pseudo-code or a programming language you have studied) that outputs ONE message in the form shown. [3 marks]

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

UK note

In AQA pseudo-code the two operations are written IntToString(…) and RealToString(…), and the message is displayed with OUTPUT.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

What you need to know

  • A string conversion operation turns a value into a string. Integer to string: 42 becomes "42". Real to string: 12.5 becomes "12.5".
  • The digits stay the same; the type changes. A string is text, so you can join it to other text and count its characters, but you can't do arithmetic with it.
  • Convert a number when it has to be joined into text, or when you need its digits as characters.
  • In Python, str() does both conversions. Python won't join a string to a number, so "Score: " + 10 stops the program with a TypeError.
  • Conversion makes a new value. The original variable is still a number.

The big picture

Converting a number to a string keeps its digits but changes its type — and the type decides what you can do with it. Integer to string turns 42 into "42"; real to string turns 12.5 into "12.5", with the decimal point as one of the characters. You convert when a number has to join other text, or when you want to work with its digits as characters, such as counting them. Once values are strings, + joins them instead of adding, so do any maths first and convert the answer. In Python, str() does both conversions.

Key points

1Do the maths first, then convert: "Total: " + str(3 + 4) gives Total: 7, but "Total: " + str(3) + str(4) gives Total: 34.
2When both sides are strings, + joins them end to end and adds no space: "3" + "4" is "34".
3Real to string keeps every digit and the decimal point. It doesn't round, and it doesn't add money-style zeros: str(7.5) is "7.5", not "7.50".
4In Python, a real that happens to be a whole number keeps its .0: str(15.0) is "15.0".
5A number's digits become countable characters only once it's a string: len(str(507)) is 3, while len(507) is an error.
6Converting to a string is not converting to binary. You get the digits you'd see on screen, as characters.
7Going the other way — from a string back to an integer or a real — is a separate operation.

Worked example

Problem

A shop program stores quantity = 3 (an integer) and price = 1.25 (a real). Write one line of Python that prints the message: You owe 3.75 for 3 items

⚠ Watch out

Joining a number straight onto text, such as print("Lives: " + lives). In Python that stops the program with a TypeError. Convert it first: print("Lives: " + str(lives)).

🧠

Memory hook

Same digits, new job. Numbers do maths; strings do sentences. Convert at the moment the number joins the sentence — after the maths is done.

✓

Check yourself

Without looking back: if n = 305, what is str(n)? What is len(str(n))? And after those lines have run, what type is n?

Flashcards

(9)
What does integer-to-string conversion do?
It turns a whole number into text made of the same digits: 42 becomes "42".
What does real-to-string conversion do?
It turns a number with a decimal part into text. The decimal point becomes one of the characters: 3.5 becomes "3.5".
Which Python function converts an integer or a real to a string?
str(). For example, str(42) gives "42" and str(3.5) gives "3.5".
Give two reasons to convert a number to a string.
To join it into a message with other text, and to treat its digits as characters — for example, to count them.
What does + do with two numbers, and with two strings?
With numbers it adds: 3 + 4 is 7. With strings it joins: "3" + "4" is "34".
What happens in Python if you run "Score: " + 10?
A TypeError — the program stops, because Python won't join text to a number. Fix it with "Score: " + str(10).
After s = str(n), what type is n?
Still a number. str() makes a new string value; it doesn't change the original variable.
Can you do arithmetic with the string "12.5"?
No. It's text. To calculate, use the number itself.
Why must you convert before using len() to count a number's digits?
len() counts the characters in a string. An integer has no length, so len(2048) is an error, but len(str(2048)) is 4.

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 30 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.