Python String Concatenation
Everyday scenario
Imagine you’re designing an invitation card. The system needs to print a message like:
1 2Dear Ali, You are invited to the Annual Science Fair at City Hall.
But instead of typing the whole message word-for-word every time, the program should join smaller pieces together: the person’s name, the event, and the location. That’s exactly what string concatenation does.
The main idea
String concatenation means joining two or more strings to form one longer string.
In Python, the simplest way is with the + operator:
1 2 3 4first_name = "Ali" last_name = "Khan" full_name = first_name + " " + last_name print(full_name)
Output:
1Ali Khan
You can also use:
- f-strings → f"Hello {name}"
- .join() → " ".join(["Ali", "Khan"])
Why this matters
Look at our invitation card:
1 2Dear Ali, You are invited to the Annual Science Fair at City Hall.
If we hardcoded this message, we’d have to rewrite it for every person. With concatenation, we can build the message by combining variables like guest_name, event, and venue.
✨ Quick recap
- Use + to join strings.
- Add " " (a space in quotes) if you want spaces between words.
- Concatenation works only with strings, numbers must be converted with str().
Try it in code
Let’s make a simple invitation:
1 2 3 4 5 6guest_name = "Ali" event = "Annual Science Fair" venue = "City Hall" invitation = "Dear " + guest_name + ",\nYou are invited to the " + event + " at " + venue + "." print(invitation)
Output:
1 2Dear Ali, You are invited to the Annual Science Fair at City Hall.
Learn with AI
If you’re using an AI assistant, try this:
Prompt:
Write a Python program that asks for a guest’s name, event name, and location, then concatenates them into a formatted invitation message.
The AI will generate ready-to-use code for you.
Practice challenge
Great work! Let’s test your understanding.
Task
Create an invitation message for:
- Guest: Sara
- Event: Book Launch
- Venue: Central Library
The output should be:
1 2Dear Sara, You are invited to the Book Launch at Central Library.
Solution
1 2 3 4 5 6guest_name = "Sara" event = "Book Launch" venue = "Central Library" message = "Dear " + guest_name + ",\nYou are invited to the " + event + " at " + venue + "." print(message)
Perfect! You’ve now learned how to join strings together in Python.
Frequently Asked Questions
String concatenation in Python refers to joining two or more strings together to form a new string.
Yes, the += operator can be used to concatenate strings in Python by appending one string to another.
String concatenation is combining strings using the + operator. Example: result = str1 + " " + str2
In Python, concat() is not a built-in function for strings. String concatenation is done using the + operator, but concat() is a function in libraries like Pandas for joining DataFrame columns.
Still have questions?Contact our support team