AppDividend
Latest Code Tutorials

Python combine strings: How to Merge Strings in Python

Python strings are an array of bytes representing Unicode characters. Strings are immutable means can not be changed after being created. In this article, we will see how to combine strings using various methods and approaches.

Python combine strings

To combine strings in Python, use the concatenation(+) operator. The concatenation operator adds a variable to another variable. Concatenating means getting a new string that contains both of the original strings.

If you see the words like combine, concat, or merge, it all means the same thing. Combine multiple different strings and make one string.

Implementation of combining strings

Declare two strings and merge them using the concat(+) operator.

minerva = "Piertotum Locomotor"

dumbledore = "Partis Temporus"

war_spells = minerva + " " + dumbledore

print(war_spells)

Output

Piertotum Locomotor Partis Temporus

And we combined two strings into one.

For numbers, the + character works as a mathematical operator that adds two numbers.

minerva = 19

dumbledore = 21

war_spells = minerva + dumbledore

print(war_spells)

Output

40

Error in string combination

If you combine string and number using the + operator, then it will give an error.

minerva = 19

dumbledore = "Explelliarmus"

war_spells = minerva + dumbledore

print(war_spells)

Output

Traceback (most recent call last):
  File "/Users/krunal/Desktop/code/pyt/database/app.py", line 5, in <module>
    war_spells = minerva + dumbledore
TypeError: unsupported operand type(s) for +: 'int' and 'str'

You can not combine string and integer variables; otherwise, you will get a TypeError.

Merge strings using the join() method

The join() is a built-in Python method that returns a string in which the sequence elements have been joined by a string separator.

The join() method combines the string that is stored in the first variable and second variable. The join() method takes only the list as its argument, and list size can be anything.

minerva = "Piertotum Locomotor"

dumbledore = "Explelliarmus"

war_spells = " ".join([minerva, dumbledore])

print(war_spells)

Output

Piertotum Locomotor Explelliarmus

Concatenate strings using % Operator

To concatenate strings in Python, you can use the % operator. This % operator can also be useful in string formatting.

minerva = "Piertotum Locomotor"

dumbledore = "Explelliarmus"

print("% s % s" % (minerva, dumbledore))

Output

Piertotum Locomotor Explelliarmus

In this example, the % operator combines the string stored in the two variables. The %s denotes string data type.

Using format() function

The string.format() function allows multiple substitutions and value formatting and lets us concatenate items within a string through positional formatting.

minerva = "Piertotum Locomotor"

dumbledore = "Explelliarmus"

op = "{} {}".format(minerva, dumbledore)

print(op)

Output

Piertotum Locomotor Explelliarmus

In this example, the format() method merges the string that is stored in the two variables and stores in another variable op.

That’s it for this tutorial.

See also

Python string concatenation

Python string substitution

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.