How to concatenate strings in python

By: On:

In this python tutorial, we will discuss how to concatenate strings in python.

We will also discuss on the below topics here

  • How to concatenate two strings in python. Can we add two strings in python?
  • How to concatenate strings and int in python
  • How to concatenate lists in python or join strings python list
  • How to concatenate two lists in python
  • How to concatenate two dictionaries in python
  • How to concatenate arrays in python
  • How to concatenate dictionaries in python
  • How to concatenate string and variable in python

If you are new to Python, you can check out What is Python? Why Python is Popular nowadays. Also, you can download and install Python and check how does Python works?


You can also create your first python hello world program.

How to concatenate strings in python

Now, let us see how to concatenate strings in python, how to concatenate two strings in python. Can we add two strings in python?

Yes, we can add or concatenate strings. There are multiple ways to concatenate strings. See below for all the approaches.


1- Using + operator

Using + operator is the most common way to concatenate strings in python. You can concatenate two strings using this operator.

The arguments should be string if you are using + operator to concatenate.

Example:

myvar1 = "Python"

myvar2 = "Guides"

# use + operator to concatenate both strings

myvar3 = myvar1 + myvar2

print(myvar3)

The out put will be

PythonGuides

Let’s run the above code and see the output

How to concatenate strings in python
How to concatenate strings in python

One important note here is python doesn’t support implicit string conversion. If you try to concatenate a string with a non-string type then it will throw an error

Let’s check this

myvar1 = 3

myvar2 = "Python"

myvar3 = myvar1 + myvar2 

print(myvar3)

Output will be an error “TypeError: unsupported operand type(s) for +: ‘int’ and ‘str'”.

myvar3 = myvar1 + myvar2
TypeError: unsupported operand type(s) for +: 'int' and 'str'

See here

TypeError unsupported operand type(s) in python

2- Using join() method

You can concatenate strings using join() method.

Example:

myvar1 = "Python"

myvar2 = "Guides"

# use join() to concatenate both strings

myvar3 = "".join([myvar1, myvar2])

print(myvar3)

The output will be

PythonGuides

See here

concatenate strings using join method

3- Using % Operator

We can also concatenate the strings using % operator

% operator can also be used to format the string.

For example:

myvar1 = "Python"

myvar2 = "Guides"

# use % operator to concatenate both strings

print("% s % s"%(myvar1,myvar2))

In the above example, We have used two %s which shows that myvar1 and myvar2 contains string data type.

The output will be

Python Guides

See here

How to concatenate string using % operator

4- Using format() function

We can also concatenate the strings using format() function. It allows us to concatenate the strings using positional formatting.

{ } (curly braces) are used along with this format() function to position the string. Each string will have one { } (curly braces).

Example:

myvar1 = "Python"

myvar2 = "Guides"

# use format() to concatenate both strings

print("{}{}".format(myvar1, myvar2))

The output will be

PythonGuides

See here

how to concatenate the string using format() function

How to concatenate strings and int in python

In Python, We cannot use + operator to concatenate one string and int type. We cannot concatenate a string with a non-string type. It will throw an error “TypeError: unsupported operand type(s) for +: ‘int’ and ‘str'”.

Below are the approaches we can use to concatenate one string and an int type.

1- Using str() function

We can use str() to concatenate one string and an int type.

Example:

myvar1 = "Python"

myvar2 = 3

# use str() to concatenate an int with a string

print(myvar1 + str(myvar2))

The output will be

Python3

See here

How to Concatenate string with int using str() in python

2- Using % Operator

We can use % Operator to concatenate one string and an int type.

Example:’

myvar1 = "Python"

myvar2 = 3

# use % operator to concatenate string with int

print("% s % s"%(myvar1,myvar2))

The output will be

Python 3
How to concatenate string with int using % operator in python

3- Using format() function

We can use format() function to concatenate one string and an int type.

One more benefit of format() is no need to convert integers into a string before concatenating the data.

Example:

myvar1 = "PythonGuides"

myvar2 = 2020

# use format() to concatenate string with int

print("{}{}".format(myvar1, myvar2))

Output will be

PythonGuides2020

See output here

format() function to concatenate one string and an int type

4- Using f strings

We can use f strings to concatenate one string and an int type.

Note: f strings supports only python 3.6 and above versions.

Example:

myvar1 = "PythonGuides"

myvar2 = 2020

# use format() to concatenate string with int

print(f"{myvar1} {myvar2}")

Output will be

PythonGuides 2020

See the output here

How to concatenate string with int using f strings in python 3.8

How to concatenate lists in python or How do I combine multiple lists into one python?

There are multiple ways to concatenate multiple lists in python.

1- Using for loop

We can use for loop to concatenate multiple lists in python.

We can traverse the second list and keep appending the elements in the first list using for loop.

Example:

myvar1 = [11, 13, 15, 17, 19]

myvar2 = [23, 25, 27, 29, 31]

# using for loop to concatenate lists

for i in myvar2 :
	myvar1.append(i)
# Printing my concatenated list
print("My concatenated list using for loop : "
      + str(myvar1))

The output will be

My concatenated list using for loop : [11, 13, 15, 17, 19, 23, 25, 27, 29, 31]

See the output here

How to concatenate multiple lists using for loop

2- Using + operator

You can use + operator to concatenate multiple lists in python.

This is one of the easy and very simple approach to concatenate multiple lists in python.

Example:

myvar1 = [11, 13, 15, 17, 19]

myvar2 = [23, 25, 27, 29, 31]

# using + operator to concatenate lists

myvar3 = myvar1 + myvar2
# Printing my concatenated list
print("My concatenated list using + operator : "
      + str(myvar3))

The output will be

My concatenated list using + operator : [11, 13, 15, 17, 19, 23, 25, 27, 29, 31]

See the output here

How to concatenate multiple lists using + operator

3- Using extend() function

You can use extend() to concatenate multiple lists in python.

Example:

mylist1 = [11, 13, 15, 17, 19]

mylist2 = [23, 25, 27, 29, 31]

# using extend() to concatenate lists

mylist1.extend(mylist2)

# Printing my concatenated list
print("My concatenated list using extend() : "
      + str(mylist1))

The output will be

My concatenated list using extend() : [11, 13, 15, 17, 19, 23, 25, 27, 29, 31]

See the output here

How to concatenate multiple lists using extend() function

4- Using * operator

Using * operator we can also concatenate multiple lists in python.

This operator will work only with python 3.6 and above versions.

Example:

mylist1 = [11, 13, 15, 17, 19]

mylist2 = [23, 25, 27, 29, 31]

# using * operator to concatenate lists

mylist3 = [*mylist1, *mylist2]

# Printing my concatenated list
print("My concatenated list using * operator : "
      + str(mylist3))

The output will be

My concatenated list using * operator : [11, 13, 15, 17, 19, 23, 25, 27, 29, 31]

See here for the output

How to concatenate multiple lists using * operator

5- Using list comprehension

We can also concatenate multiple lists using list comprehension.

Example:

mylist1 = [11, 13, 15, 17, 19]

mylist2 = [23, 25, 27, 29, 31]

# using list comprehension to concatenate lists

mylist3 = [b for a in [mylist1, mylist2] for b in a]

# Printing my concatenated list
print("My concatenated list using list comprehension : "
      + str(mylist3))

The output will be

My concatenated list using list comprehension : [11, 13, 15, 17, 19, 23, 25, 27, 29, 31]

See the output here

How to concatenate multiple lists using list comprehension

6- Using itertools.chain() method

We can also concatenate multiple lists itertools.chain() method.

Note: We need to import itertools namespace.

Example:

import itertools

mylist1 = [11, 13, 15, 17, 19]

mylist2 = [23, 25, 27, 29, 31]

# using itertools.chain() method to concatenate lists

mylist3 = list(itertools.chain(mylist1, mylist2))

# Printing my concatenated list
print("My concatenated list using itertools.chain() method : "
      + str(mylist3))

The output will be

My concatenated list using itertools.chain() method : [11, 13, 15, 17, 19, 23, 25, 27, 29, 31]

See the output here

How to concatenate multiple lists using itertools.chain() method

How to concatenate two dictionaries in python

Here, we will discuss how to concatenate two dictionaries or merging two dictionaries in python.

There are few ways to concatenate two dictionaries in python.

1- Using update() function

We can concatenate two dictionaries in python using update() method.

Example:

mydict1 = {'a': 10, 'b': 8}
mydict2 = {'d': 6, 'c': 4}
# Python code to concatenate dict using update() method
def Merge(mydict1, mydict2):
    return (mydict2.update(mydict1))

print(Merge(mydict1, mydict2))

# changes made in dict2
print(mydict2)

The output will be

None
{'d': 6, 'c': 4, 'a': 10, 'b': 8}

See the output here

How to concatenate two dictionaries using update() method

2- Using ** (double star)

You can concatenate two dictionaries in python using ** (double star).

Example:

mydict1 = {'a': 10, 'b': 8}
mydict2 = {'d': 6, 'c': 4}

def Merge(mydict1, mydict2):
    output = {**mydict1, **mydict2}
    return output

mydict3 = Merge(mydict1, mydict2)
print(mydict3)

The output will be

{'a': 10, 'b': 8, 'd': 6, 'c': 4}

See the output here

How concatenate two dictionaries using double star

How to concatenate arrays in python

We can use numpy.concatenate() to concatenate multiple numpy arrays.

Example:

import numpy as test

a = test.arange(5,9)
b = test.arange(2,4)
c= test.arange(6,8)
test.concatenate([a,b,c])

Oops, after executing the above code i got the below error

ModuleNotFoundError: No module named ‘numpy’

See the below output

ModuleNotFoundError: No module named 'numpy'

So to fix the above error you need to install ‘numpy’ with pip install numpy command.

To run the above command you need to open the command prompt(Run as administrator mode) and go to the path where python is installed and then run the above command.

See below

How to install numpay in windows 10

Now after installing numpy, you can able to run the above code which will concatenate both the arrays.

You may like following below Python Tutorials:

Conclusion:


Python is the most popular open-source object-oriented programming language and it is easy to learn and syntax wise it is very simple.

This python tutorial explains the below points:

  • How to concatenate strings in python
  • How to concatenate two strings in python. Can we add two strings in python?
  • How to concatenate strings and int in python
  • How to concatenate lists in python or join strings python list
  • How to concatenate two lists in python
  • How to concatenate two dictionaries in python
  • How to concatenate arrays in python
  • How to concatenate dictionaries in python
  • How to concatenate string and variable in python

Leave a Comment