+

Python | Add one line to another

Method # 1: Using + = operator

This operator can be used to perform this particular string concatenation task. It is much easier than traditional methods that are used in other languages, such as using a special function to accomplish this particular task.

# Python code for demo
# Adding one line to another
# Using the + = operator

 
# initialization string

test_string = "GFG"

 
# initializing add_string

add_string = "is best"

 
# print ori the original line

print ( "The original string: " + str (test_string))

 
# print original add line

print ( "The add string:" + str (add_string))

 
# Using the + = operator
# adding one line 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: Using join()

You can also perform this string concatenation task using the join function ... The advantage of this method over the above method is that we have many lines to concatenate, not just two.

# Python code for demonstration
# Adding one line to another
# Using join ()

 
# initialization string

test_string = "GFG"

  
# initializing add_string

add_string = "is best"

 
# print original line

print ( "The original string:" + str (test_string))

 
# print original add line

print ( " The add string: " + str (add_string))

 
# Using join ()
# adding one line to another

res = "". join ((test_string, add_string))

 
# print result

pri nt ( "The concatenated string is:" + res)

Output:

 The original string: GFG The add string: is best The concatenated string is: GFG is best 
Get Solution for free from DataCamp guru