String Concatenation in Python?
In Python, string concatenation is the process of combining two or more strings into a single string.
Concatenating strings using addition ('+') operator
In Python, you can concatenate (combine) two or multiple strings using '+' operator.
In the following example, the string variables first_name, and last_name are combined into a single string using '+' operator. Combined string value is passed to the string variable fullname fullname = first_name + " " +last_name.
Note: Space (" ") is used to keep space between first_name and last_name.
Please take a closer look at the following example:
Example of using ('+') operator to combine strings
Output of the above example
John Smith
Concatenating strings using addition & assignment ('+=') operator
In Python, you can concatenate (combine) two or multiple strings using '+=' operator.
In the following example, the string variable my_string has the string value "Python is ". We can add more string data to the variable just using the '+=' operator.
my_string += "fun." is used to add the text "fun." to the existing string value ("Python is ") of the 'my_string' variable.
Please take a closer look at the following example:
Example of using ('+=') operator to combine strings
Output of the above example
Python is fun.