Concatenation Strings
What You'll Learn: In this tutorial, you'll discover how to concatenate, or combine, two strings in Python using the + operator. It's like sticking two pieces of text together!
What is String Concatenation?
String concatenation is the process of joining two or more strings end-to-end to form a new string.
Using the + Operator
To concatenate two strings, use the + operator.
Example Code:
a = "Hello"
b = "World"
c = a + b
print(c)
What's Happening Here?
ais the first string.bis the second string.cis the result of concatenatingaandb.
Output: HelloWorld
Adding a Space Between Strings
To add a space between the strings, include a space character " " in the concatenation.
Example Code:
a = "Hello"
b = "World"
c = a + " " + b
print(c)
Output: Hello World
Try It Yourself: Fun Exercises
- Combine Your First and Last Name:
- Create two strings with your first and last name.
- Concatenate them with a space in between.
- Greeting Message:
- Create a string with a greeting (e.g., "Hi").
- Concatenate it with your name to form a personalized greeting.
Summary:
In this Python tutorial, we learned how to concatenate strings using the + operator. We can combine two or more strings and even add spaces between them. String concatenation is a handy tool for creating messages and joining text in your programs. Keep experimenting and have fun with strings in Python!