23
loading...
This website collects cookies to deliver better user experience
+=
operatorstring.join()
methodf-strings
+=
(plus equal) operator is similar to any other language that concatenates two strings and returns a new string in Python without changing the original string.# Python program to append to a string using += operator
first_name="Bing"
last_name=" Chandler"
# printing first name
print("The first name is: " + str(first_name))
# printing last name
print("The last name is: " + str(last_name))
# Using += operator adding one string to another
first_name += last_name
# print concatenated string
print("The concatenated string is: " + first_name)
The first name is: Bing
The last name is: Chandler
The concatenated string is: Bing Chandler
join()
method. It has an advantage over the +=
operator as it can append multiple strings or a list of strings. join()
function, which concatenates them together and returns to the output string.# Python program to append a string using join() function
first_name="Bing"
last_name=" Chandler"
# printing first name
print("The first name is: " + str(first_name))
# printing last name
print("The last name is: " + str(last_name))
# Using join() method to append a string
result= "".join([first_name,last_name])
# print concatenated string
print("The concatenated string is: " + result)
The first name is: Bing
The last name is: Chandler
The concatenated string is: Bing Chandler
# Python program to append a string using f-string
first_name="Bing"
last_name=" Chandler"
# printing first name
print("The first name is: " + str(first_name))
# printing last name
print("The last name is: " + str(last_name))
# Using f-string to append a string
result= f"{first_name}{last_name}"
# print concatenated string
print("The concatenated string is: " + result)
The first name is: Bing
The last name is: Chandler
The concatenated string is: Bing Chandler