Write a Python Program to Concatenate Two Strings

Hi, in this article lets look how to concatenate two strings using python program.


There are three ways to concatenate the string 

1) Using the + (Plus)

2) Using For Loop (Basic Method)

3) Using built in function join()

Lets try to concatenate two strings using + (Plus) Symbol

 

 

combine two stings

 

Python Program to Concatenate Two Strings using +

Lets take two variables and ask user to enter two strings and then take another string3 and concatenate two strings using plus and finally print string3

 

string1 = input("Enter First String : ")
string2 = input("Enter Second String : ")

string3 = string1 + string2

print("Combination of  two string is : ", string3)


Python Program to Concatenate Two Strings using For Loop

Lets take two variables and ask user to enter two strings, Once we get two strings add the string2 to string1 using for loop by traversing each and every letter to string. Finally print the string1.

 

string1 = input("Enter First String : ")
string2 = input("Enter Second String : ")

for letter in string2:
    string1 += letter
    
print("Combination of two string is : ", string1)


Python Program to Concatenate Two Strings using Built In function join() 

Two strings can be combined to one using built in string function join(). Lets take two variables and ask user to enter two strings, Once we get two strings pass a list of string1 and string2 separated by comma to join function.

 

string1 = input("Enter First String : ")
string2 = input("Enter Second String : ")

string3 = "".join([string1, string2])

print("Combination of two string is : ", string3)

 

Output 1: 

Enter First String : how are you
Enter Second String : ????

Combination of two string is :  how are you????

 

Conclusion : Try to run the program by yourself and comment down below if you have any issue.



Post a Comment

0 Comments