Sunday, September 22, 2024
Google search engine
HomeLanguagesPython | Add one string to another

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.

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

Strings in Python are immutable, which means 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.

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. 

Python3




# initializing string
test_string = "GFG"
 
# initializing add_string
add_string = " is best"
 
#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. 

Python3




# initializing string
test_string = "GFG"
 
# initializing add_string
add_string = " is best"
 
# 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!

f-String to Insert Characters

In this example, we are adding a string to another string or character using the Python f-string.

Python3




# initializing string
test_string = "GFG"
 
# initializing add_string
add_string = " is best"
 
# 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

In this example, we are adding a string to another string or character using the Python __add__ method.

Python3




# initializing string
test_string = "GeeksforLazyroar"
 
# initializing add_string
add_string = " is best"
 
# 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 : GeeksforLazyroar is best
GeeksforLazyroar is best!

Python add Character to String using format() 

In this example, we are adding a string or character to another string using the Python format() function.

Python3




# initializing string
test_string = "GeeksforLazyroar"
 
# initializing add_string
add_string = " is best"
 
# 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 : GeeksforLazyroar is best
GeeksforLazyroar 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.

Python3




# Initialize two strings
test_string = "GeeksforLazyroar"
add_string = " is best"
 
# 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: GeeksforLazyroar is best
GeeksforLazyroar is best!

RELATED ARTICLES

Most Popular

Recent Comments