Python Concatenate String and int

Filed Under: Python

Python supports string concatenation using + operator. In most of the programming languages, if we concatenate a string with an integer or any other primitive data types, the language takes care of converting them to string and then concatenate it. However, in Python, if you try to concatenate string and int using + operator, you will get a runtime error.

Python Concatenate String and int

Let’s look at a simple example to concatenate string and int using + operator.

s = 'Year is '

y = 2018

print(s + y)


Traceback (most recent call last):
  File "/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/strings/", line 5, in <module>
    print(s + y)
TypeError: can only concatenate str (not "int") to str

So how to concatenate string and int in Python? There are various other ways to perform this operation.

Using str() function

The easiest way is to convert int to a string using str() function.

print(s + str(y))

Output: Year is 2018

Using % Operator

print("%s%s" % (s, y))

Using format() function

We can use string format() function too for concatenation of string and int.

print("{}{}".format(s, y))

Using f-strings

If you are using Python 3.6 or higher versions, you can use f-strings too.

You can checkout complete python script and more Python examples from our GitHub Repository.


  1. M P ANUPAMA says:

    Write a python program that asks the user how many coins of various types they have, and then prints the total amount of money in rupees.

  2. Anupinder singh says:

    Hey Pankaj, thanks for sharing this article. This helped me to have a better understanding of formats used with print function

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages