Python | Concatenate two strings and assign in another string by using + operator

Here, we will learn how to concatenate two strings using + (plus) operator and assign the result to another string in Python? By IncludeHelp Last updated : February 12, 2024

Problem statement

Write a Python program to concatenate two strings and assign in another string.

Concatenating two strings and assigning in another string

In Python, the plus (+) operator can be used to concatenate two or more strings and to assign the result in another string using a separate string variable.

Example

The below example shows the concatenation and assignment process with the help of sample input and output values.

Input:
str1 = "Hello"
str2 = "World"

str3 = str1 + ' ' + str2
Output:
str3 = "Hello World"

Python program to concatenate two strings and assign in another string

In this program, there are two strings "Hello" and "World" and we have to concatenate these two strings along with a space between the strings and assign the result to the third string.

# Python code for concatenating two strings
# and assigning in another string

# Input strings
str1 = "Hello, World!"
str2 = "How're you?"

# Concatenate and assign in another string
str3 = str1 + str2

# print strings
print("str1: ", str1)
print("str2: ", str2)
print("str3: ", str3)

# Putting space between str1 and str2
str3 = str1 + " " + str2
print("str3: ", str3)

Output

The output of the above program is:

str1:  Hello, World!
str2:  How're you?
str3:  Hello, World!How're you?
str3:  Hello, World! How're you?

Python String Programs »

To understand the above example, you should have the basic knowledge of the following Python topics:

Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.