
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- Concatenate string with numbers in MySQL?
- How to concatenate string vectors separated with hyphen in R?
- How to concatenate variable in a string in jQuery?
- What is the preferred way to concatenate a string in Python?
- How to concatenate numerical vectors and a string to return a string in R?
- How to extract numbers from a string in Python?
- Concatenate null to a string in Java
- How to concatenate multiple string variables in JavaScript?
- How to sum a comma separated string (string with numbers) in MySQL?
- How to concatenate a std::string and an int in C++?
- How to extract numbers from a string using Python?
- How to concatenate two strings in Python?
- How to concatenate MySQL distinct query results into a string?
- Java Program to Concatenate String.
- How to concatenate the string value length -1 in JavaScript.
