How to Add Two Strings in Python
To add two strings in Python, you can use f-string, the + operator, or the format() function.
The following examples show how to add strings in Python.
Using + Operator
We can use the + operator to add two strings in Python.
Suppose we have the following strings:
# Declare strings
str1 = "Hello "
str2 = "World"
# Add strings
result = str1 + str2
# Show string
print("String:", result)
Output: 👇️
String: Hello World
In this example, we use the + operator to concatenate str1 and str2.
Using f-string
We can use f-strings to add two strings in Python.
Suppose we have the following strings:
# Declare strings
str1 = "Hello"
str2 = "World"
# Add strings
result = f"{str1} {str2}"
# Show string
print("String:", result)
Output: 👇️
String: Hello World
In this example, we use an f-string to concatenate str1 and str2.
Using format() Function
We can use the format() function to add two strings in Python.
Suppose we have the following strings:
# Declare strings
str1 = "Hello {}"
str2 = "World"
# Add strings
result = str1.format(str2)
# Show string
print("String:", result)
Output: 👇️
String: Hello World
In this example, we use the format() function to concatenate str1 and str2.