Insert a number in string - Python
Last Updated :
22 Apr, 2025
We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The number is 42". Let's explore some common and efficient methods of doing it with examples:
Using f-Strings
Introduced in Python 3.6, f-strings offer a fast and readable way to embed variables directly into strings. They’re evaluated at runtime and are considered the most efficient method.
Python
n = 42
res = f"The number is {n}"
print(res)
Explanation: The f before the string allows expressions inside {} to be evaluated. It’s concise, readable, and performs faster than older methods.
Using String Concatenation
This is the most basic method where we convert the number to a string and join it with the original string using the + operator.
Python
s = "The number is "
n = 42
res = s + str(n)
print(res)
Explanation:
- str(n) converts the integer n to a string, allowing it to be concatenated with the original string s.
- + operator is used to concatenate the two strings, resulting in the combined string.
It's a classical method that uses format specifiers like %d for integers, allowing the insertion of numbers at specific placeholders.
Python
s = "The number is %d"
n = 42
res = s % n
print(res)
Explanation: %d in the string acts as a placeholder for an integer, which is replaced with the value of num.
This method allows us to insert variables into a string by placing curly braces {} as placeholders. This method offers greater flexibility and readability compared to older formatting methods like % operator, supporting both positional and keyword arguments
Python
s = "The number is {}"
n = 42
res = s.format(n)
print(res)
Explanation: Curly braces {} serve as placeholders, and format(num) replaces them with the number, resulting in a clean and formatted string.
Also read: str.format(), f-strings.
Similar Reads
List of strings in Python A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona =
2 min read
Python - Retain Numbers in String Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones.Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to for
2 min read
Integer to Binary String in Python We have an Integer and we need to convert the integer to binary string and print as a result. In this article, we will see how we can convert the integer into binary string using some generally used methods.Example: Input : 77Output : 0b1001101Explanation: Here, we have integer 77 which we converted
3 min read
Python - Extract numbers from list of strings We are given a list of string we need to extract numbers from the list of string. For example we are given a list of strings s = ['apple 123', 'banana 456', 'cherry 789'] we need to extract numbers from the string in list so that output becomes [123, 456, 789].Using Regular ExpressionsThis method us
2 min read
Insert a Variable into a String - Python The goal here is to insert a variable into a string in Python. For example, if we have a variable containing the word "Hello" and another containing "World", we want to combine them into a single string like "Hello World". Let's explore the different methods to insert variables into strings effectiv
2 min read