How will you concatenate multiple string together in Python?
Learn more
In Python, you can concatenate multiple strings together using the
+
operator or by using string formatting.
str1 = "Hello"
str2 = " "
str3 = "World"
result = str1 + str2 + str3
print(result)
# Output: Hello World
Using + Operator
name = "John"
age = 30
result = "My name is {} and I'm {} years old".format(name, age) print(result)
# Output: My name is John and I'm 30 years old
Concatenating strings using string formatting
We have created two variables name and age and used string formatting to concatenate them together into a single string.
The curly braces
{}
act as placeholders for the variables, which are then passed to the
format()
method
The resulting string is stored in the
result
variable and printed to the console.