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
Method 6: Using a list comprehension
Python3
# Initialize two strings test_string = "Geeksforgeeks" add_string = " is best" # Using a list comprehension res = "".join([test_string, add_string]) # Print result print ( "The concatenated string is:" , res) |
The concatenated string is: Geeksforgeeks is best
Step-by-step algorithm:
- Initialize the string test_string with the given input.
- Initialize the string add_string with the given input to be added to test_string.
- Use a list comprehension to concatenate the two strings. The list contains only the two input strings, which are joined together using the
- join() method with an empty string as the separator. The resulting concatenated string is assigned to the variable res.
- Print the concatenated string.
Time complexity:
The time complexity of the algorithm is O(n), where n is the total length of the two input strings test_string and add_string. This is because the list comprehension iterates over both strings once to create the list of strings to be joined. The join() method also has a linear time complexity with respect to the length of the list.
Auxiliary space complexity:
The auxiliary space complexity of the algorithm is O(n), where n is the total length of the two input strings test_string and add_string. This is because the list comprehension creates a list of two strings that are both the same length as their input strings. The join() method also creates a new string that is the same length as the two input strings combined. Therefore, the space complexity of the algorithm is linear with respect to the length of the input strings.
Please Login to comment...