What is concatenation in Python?
How Can We Help?
Teacher,
Cat enthusiast
- The 2022 End of Year Website Update - 01/01/2023
- Celebrating Ada Lovelace day - 10/10/2022
- Website update 5/9/22 - 05/09/2022
Though a complicated word, concatenation is actually really simple. It simply means joining things together and in Python we can use concatenation to join text or strings together.
For example:
a = "Hello "
b = "World"
print (a , b)
In this example Hello World will be output to the screen.
Instead of the comma we can also use the + symbol to concatenate strings in Python.
For example:
a = "Hello "
b = "World"
print(a + b)
This will produce exactly the same output on the screen – Hello World.
Concatenation is often used to display string variables to the screen like the example below:
name = input("Please enter your name")
print("Hello ",name, " nice to meet you!")
In this example, a string input is taken from the user and stored in the variable called name.
The program outputs “Hello” and then concatenates (adds together) the string variable name and then the text ” nice to meet you!” after it.