Open In App

Python | Add one string to another

Improve
Improve
Like Article
Like
Save
Share
Report

The concatenation of two strings has been discussed multiple times in various languages. But the task is how to add to a string in Python or append one string to another in Python.

Example

Input: 'GFG' + 'is best'                     
Output: 'GFG is best'
Explanation: Here we can add two string using "+" operator in Python

Append a String in Python

Python Strings are immutable, meaning they cannot be directly modified. However, you can use several techniques to achieve the effect of appending to a string. Knowledge of performing this task has many applications. Let’s discuss certain ways in which this can be performed, as there are various ways to Append to Strings in Python here we are discussing some generally used methods to add to a string in Python those are the following.

Create String in Python

The below code initializes two strings: “GFG” assigned to the variable `test_string` and ” is best” assigned to the variable `add_string`.

Python3




# initializing string
test_string = "GFG"
  
# initializing add_string
add_string = " is best"


Concatenate Strings in Python

This operator can be used to perform this particular task of concatenating the string or character. This is quite simpler than the traditional methods that are employed in other languages, like using a dedicated function to perform this particular task. In Python, you can use the + operator to append strings in Python, making the code more concise and readable.

Example : In this example the below code concatenates the strings “GFG” and ” is best,” prints the result (“GFG is best”), and then appends an exclamation mark to it, printing the final string (“GFG is best!”).

Python3




#adding test_string to add_string
print(test_string+add_string)
res = test_string+add_string
  
#adding character to a string
print(res+"!")


Output

GFG is best
GFG is best!

Join a List of Strings Into One String

One can also perform this very task of the concatenation of strings or characters 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.

Example : In this example the below code initializes two strings, “GFG” and ” is best,” concatenates them using the `join()` method, and prints the result. Then, an exclamation mark is added to the concatenated string, and the updated string is printed.

Python3




# Using join()
# adding one string to another
res = "".join((test_string, add_string))
  
# print result
print("The concatenated string is : " + res)
  
ans = "".join((res,"!"))
  
#print after adding character
print(ans)


Output:

The concatenated string is : GFG is best
GFG is best!

Python Append String Using F-String to Insert Characters

In Python, use f-strings to add characters to a string by embedding variables or expressions within curly braces, allowing dynamic content insertion with a clean syntax.

Example : In this example , In below code Python f-string uses to concatenate the strings “GFG” and ” is best”. It first prints the concatenated string, and then adds an exclamation mark to it and prints the final result and thus append strings in Python.

Python3




# Using f-string
# adding one string to another
res = f'{test_string}{add_string}'
  
# print result
print("The concatenated string is : " + res)
  
#adding one string to a character
ans = f'{res}{"!"}'
print(ans)


Output:

The concatenated string is : GFG is best
GFG is best!

Add Character using the __add__ Method

The `__add__` method in Python is used to define the behavior of the `+` operator for objects. It allows customization of addition or concatenation operations for specific object types.

Example : In this example the below code initializes strings “Geeksforgeeks” and ” is best,” concatenates them using `__add__`, prints the result, then string adds an exclamation mark to it and prints the final output and append a string Python.

Python3




# Using __add__ 
# adding one string to another
res = test_string.__add__(add_string)
  
# print result
print("The concatenated string is : " + res)
  
#using __add__ to add one character to a string
ans = res.__add__('!')
print(ans)


Output:

The concatenated string is : Geeksforgeeks is best
Geeksforgeeks is best!

Python Add Character to String using format() 

The `format()` method in Python enables adding characters or strings to a base string by using placeholders. It provides a concise and flexible way to construct and modify strings dynamically.

Example : In this example the below code initializes strings, concatenates them using the Python format() method, and prints the result. It then adds an exclamation mark and append a string Python and prints the final output.

Python3




# Using format
# adding one string to another
res = "{}{}".format(test_string, add_string)
  
# print result
print("The concatenated string is : " + res)
  
#adding one character to a string
ans = "{}{}".format(res,'!')
print(ans)


Output:

The concatenated string is : Geeksforgeeks is best
Geeksforgeeks is best!

Python add Character to String using a List Comprehension

The code concatenates two strings or characters, “test_string” and “add_string”, using a list comprehension and the join() function. The resulting concatenated string is stored in the variable res and then printed as output.

Example : In this example the below code initializes strings, concatenates them using list comprehension and `join`, prints the result, then adds an exclamation mark and prints the final output.

Python3




# Using a list comprehension
res = "".join([test_string, add_string])
  
# Print result
print("The concatenated string is:", res)
  
ans  = "".join([res,"!"])
print(ans)


Output

The concatenated string is: Geeksforgeeks is best
Geeksforgeeks is best!


Last Updated : 28 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads