Output
Concatenation means joining two or more strings end to end to make one longer string. In Python 3 you do it with the plus operator: put + between two strings and the result is one string holding the characters of the first followed by the characters of the second.
You can join string literals, variables that hold strings, or a mixture of both, and you can chain as many pieces as you like in one expression. A quiz program might store a player's first name and surname separately, then join them into one name to display:
first_name = "Priya"
last_name = "Raman"
full_name = first_name + " " + last_name
print(full_name)Output:
Priya Raman
Plus only glues the pieces together, so it adds nothing of its own. Any space you want must be typed inside one of the string literals, as the " " above does. Leave it out and the two words are squashed together into one, which looks wrong but is not an error, so the program still runs.
Plus expects two strings, and it can also add two numbers, but it cannot do both jobs at once. Gluing a piece of text to a number therefore stops the program with an error:
team = "Falcons"
points = 42
print(team + points)Output:
TypeError: can only concatenate str (not "int") to str (line 3)
Python will not guess which job you meant, so it refuses to run that line. Until you learn how to convert a number into a string, output the text and the number as separate items with a comma between them, which writes them on the same line with a single space in between. The number is printed as a value and needs no conversion.
OCR GCSE Computer Science · Output
OCR GCSE Computer Science · Input
OCR GCSE Computer Science · String Manipulation