Python | Add one string to another

Concatenation of two strings have been discussed multiple times over various languages. But the task of adding one string to other is quite easy task in Python. Knowledge of performing this task has many application. Let’s discuss certain ways in which this can be performed.

Method #1 : Using += operator

This operator can be used to perform this particular task of concatenating the string. This is quite simpler than the traditional methods that are employed in other languages, like using a dedicated function to perform this particular task.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# Adding one string to another
# Using += operator
  
# initializing string 
test_string = "GFG"
  
# initializing add_string
add_string = " is best"
  
# printing original string 
print("The original string : " + str(test_string))
  
# printing original add string 
print("The add string : " + str(add_string))
  
# Using += operator
# adding one string to another 
test_string += add_string
  
# print result
print("The concatenated string is : " + test_string)

chevron_right


Output :

The original string : GFG
The add string :  is best
The concatenated string is : GFG is best

 

Method #2 : Using join()

One can also perform this very task of the concatenation of string using the join function. The advantage this method holds over the above method is when we have many strings to concatenate rather than just two.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# Adding one string to another
# Using join()
  
# initializing string 
test_string = "GFG"
  
# initializing add_string
add_string = " is best"
  
# printing original string 
print("The original string : " + str(test_string))
  
# printing original add string 
print("The add string : " + str(add_string))
  
# Using join()
# adding one string to another 
res = "".join((test_string, add_string))
  
# print result
print("The concatenated string is : " + res)

chevron_right


Output :

The original string : GFG
The add string :  is best
The concatenated string is : GFG is best


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.