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.
# 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) |
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.
# 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) |
The original string : GFG The add string : is best The concatenated string is : GFG is best
Recommended Posts:
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
- Python | Sorting string using order defined by another string
- String slicing in Python to rotate a string
- Python | Check if a given string is binary string or not
- Python String | max()
- Python String | min()
- Python String
- Python string | isdecimal()
- String partition() in Python
- string.punctuation in Python
- Python String | find()
- Python string | swapcase()
- Python string | printable()
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.



