Skip to content

Write a Python program to concatenate two strings

  • by

The + operator can be used to concatenate two different strings in Python. Here are the all options to do it.

  • Concatenate String using + Operator
  • Using list
  • Using join()
  • Use the * operator

Appending the same string to a string can be done using the * operator, as follows:

str=str * x

Where x is the number of times the string str is appended to itself.

Python program to concatenate two strings

Simple example code concatenates strings in Python.

str1 = "Hello"
str2 = "World"

# +
print(str1 + str2)

# Appending 
print(str1 * 3)

# join
strOne = "".join([str1, str2])
print(strOne)

Output:

Write a Python program to concatenate two strings

Concatenate String using List

print("Enter Two Strings: ", end="")
strOne = input()
strTwo = input()

oneList = []
twoList = []
tmpList = []
oneList[:0] = strOne
twoList[:0] = strTwo
tmpList[:0] = strOne
oneLen = len(strOne)
twoLen = len(strTwo)
tmpLen = oneLen

for i in range(twoLen):
    oneList.insert(oneLen, twoList[i])
    oneLen = oneLen+1

print("\nFirst String after Concatenating Second: ", end="")
for i in range(len(oneList)):
    print(end=oneList[i])

for i in range(tmpLen):
    twoList.insert(twoLen, tmpList[i])
    twoLen = twoLen+1

print("\nSecond String after Concatenating First: ", end="")
for i in range(len(twoList)):
    print(end=twoList[i])
print()

Do comment if you have any doubts or suggestions on this Python string topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading