How to concatenate a string with numbers in Python?


In this article, we are going to find out how to concatenate a string with numbers in Python.

The first approach is by typecasting the number into a string using typecasting. After typecasting the number, we can concatenate using the ‘+’ operator.

Type casting or type conversion in Python are terms used to describe the transformation of one data type into another. For type casting in Python, a large number of functions and methods are supported, including: int(), float(), str(), ord(), hex(), oct(), tuple(), set(), list(), dict(), etc.

Example 1

In the example given below, we are taking a string and number as input and we are concatenating by typecasting and ‘+’ operator 

name = "Tutorialspoint"
year = 2022

print("The given string is")
print(name)

print("The given number is")
print(year)

print("The concatenated string is")
res = name + str(year)
print(res)

Output

The output of the above example is as shown below −

The given string is
Tutorialspoint
The given number is
2022
The concatenated string is
Tutorialspoint2022

Example 2

In Python 2, you can also use backtick(``) to surround the number and get the same result with numbers and strings. Note that backticks have been removed from Python 3 as shown in the following example 

>>> a = "string"
>>> b = 1
>>> print a + `b`
string1

Using f strings

The second approach is by f strings. F strings in Python are implemented by adding the letter f before the start of quotes of the string. Inside the quotes, we should write the strings or numbers that we want to concatenate in curly braces.

Example

In the example given below, we are taking a string and number as input and we are concatenating them using the f strings.

name = "Tutorialspoint"
year = 2022

print("The given string is")
print(name)

print("The given number is")
print(year)

print("The concatenated string is")
res = f'{name}{year}'
print(res)

Output

The output of the above example is as shown below−

The given string is
Tutorialspoint
The given number is
2022
The concatenated string is
Tutorialspoint2022

Using format() method

The third approach is by using the format() method. It is mostly used in Python 2.x versions, but we can also use it in python 3.x versions. We will just add empty flower braces inside the quotes then call the format () function and call the string and number.

Example

In the example given below, we are taking a string and an integer as input and we are concatenating them using format() method 

name = "Tutorialspoint"
year = 2022

print("The given string is")
print(name)

print("The given number is")
print(year)

print("The concatenated string is")
res = '{}{}'.format(name,year )
print(res)

Output

The output of the above example is given below−

The given string is
Tutorialspoint
The given number is
2022
The concatenated string is
Tutorialspoint2022

Using % operator

The fourth approach is by using %, we should write %s for each integer and string inside the quotes as we want the final output as a string, then we will add the name of the number and string we are using with a %.

Example

In the example given below, we are taking a string and an integer as input and we are concatenating them using % 

name = "Tutorialspoint"
year = 2022

print("The given string is")
print(name)

print("The given number is")
print(year)

print("The concatenated string is")
res = '%s%s' % (name, year)
print(res)

Output

The output of the above example is as shown below −

The given string is
Tutorialspoint
The given number is
2022
The concatenated string is
Tutorialspoint2022

Updated on: 07-Dec-2022

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements