Python | Add one string to another
The concatenation of two strings has been discussed multiple times in various languages. But the task is to append one string to another in Python. Knowledge of performing this task has many applications. Let’s discuss certain ways in which this can be performed.
Method 1: Concatenate Strings Into a String using the += 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.
Python3
# 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) |
Output :
The original string : GFG The add string : is best The concatenated string is : GFG is best
Method 2: Join Strings Into a String using join()
One can also perform this very task of the concatenation of strings using the Python join function. The advantage this method holds over the above method is when we have many strings to concatenate rather than just two.
Python3
# 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) |
Output:
The original string : GFG The add string : is best The concatenated string is : GFG is best
Method 3: Append one string to another using f-string
In this example, we are adding a string to another string using the Python f-string.
Python3
# 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 f-string # adding one string to another res = f '{test_string}{add_string}' # print result print ( "The concatenated string is : " + res) |
Output:
The original string : GFG The add string : is best The concatenated string is : GFG is best
Method 4: Add one String to another String using the __add__ method
In this example, we are adding a string to another string using the Python __add__ method.
Python3
# initializing string test_string = "Geeksforgeeks" # 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 __add__ # adding one string to another res = test_string.__add__(add_string) # print result print ( "The concatenated string is : " + res) |
Output:
The original string : Geeksforgeeks The add string : is best The concatenated string is : Geeksforgeeks is best
Method 5: Python adds string using format()
In this example, we are adding a string to another string using the Python format() function.
Python3
# initializing string test_string = "Geeksforgeeks" # 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 format # adding one string to another res = "{}{}" . format (test_string, add_string) # print result print ( "The concatenated string is : " + res) |
Output:
The original string : Geeksforgeeks The add string : is best The concatenated string is : Geeksforgeeks is best
Please Login to comment...