Python | Insert a number in string

Sometimes, while dealing with strings, we may encounter a problem in which we might have a numeric variable whose value keeps changing and we need to print the string including that number. Strings and numbers being different data types have to be solved in different ways. Let’s discuss specific ways in which this problem can be solved in Python.

Input: BeginnerBeginner
Output: Beginner4Beginner
Explanation: We inserted the number 4 in the input string.

Insert a Number in The String

Below are the methods that we will cover in this article:

  • Using Type Conversion
  • Using the %d operator
  • Using join() method
  • Using format() method
  • Using f-string

Insert a number in String using Type Conversion

The simplest way this task can be performed is by explicitly converting the integer into a string datatype using the basic type conversion and then we will concatenate the string to the appropriate position. 

Python3




# initializing string
test_str = "Beginner"
 
# initializing number
test_int = 4
 
# printing original string
print("The original string is : " + test_str)
 
# printing number
print("The original number : " + str(test_int))
 
# using type conversion
# Inserting number in string
res = test_str + str(test_int) + test_str
 
# printing result
print("The string after adding number is : " + str(res))


Output

The original string is : Beginner
The original number : 4
The string after adding number is : Beginner4Beginner

Inserting values into strings using the %d operator

This operator can be used to format the string to add the integer. The “d” represents that the datatype to be inserted into the string is an integer. This can be changed according to the requirements. 

Python3




test_str = "Beginner"
 
# initializing number
test_int = 4
 
# printing original string
print("The original string is : " + test_str)
 
# printing number
print("The original number : " + str(test_int))
 
# using % d operator
# Inserting number in string
res = (test_str + "% d" + test_str) % test_int
 
# printing result
print("The string after adding number is : " + str(res))


Output

The original string is : Beginner
The original number : 4
The string after adding number is : Beginner 4Beginner

Inserting values into strings using join() method

Here with the help of the join() method in Python insert the number in a string

Python3




test_str = "Beginner"
 
# initializing number
test_int = 4
 
# printing original string
print("The original string is : " + test_str)
 
# printing number
print("The original number : " + str(test_int))
 
# Inserting number in string
res=[test_str]*2
res=str(test_int).join(res)
 
# printing result
print("The string after adding number is : " + str(res))


Output

The original string is : Beginner
The original number : 4
The string after adding number is : Beginner4Beginner

Insert a number in string using format() method

The format method is also be used to insert numbers in a string by passing the variable which holds the numeric value in the parentheses of the format method

Python3




test_str = "Beginner"
  
# initializing number
test_int = 4
  
# printing original string
print("The original string is : {}".format(test_str))
  
# printing number
print("The original number : {}".format(test_int))
  
# Using format() method
res = "{}{}{}".format(test_str, test_int, test_str)
  
# printing result
print("The string after adding number is : {}".format(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original string is : Beginner
The original number : 4
The string after adding number is : Beginner4Beginner

Time Complexity: All the above methods have a Time Complexity of O(1) as it is just a simple concatenation or formatting of strings.
Auxiliary Space: Space Complexity of O(n) as we create a new variable ‘res’ to store the new string.

Insert a number in string using f-string

Initialize the string variable test_str with the value “Beginner” and an integer variable test_int with value 4 then the original string using the print() function and .format() method after it Pprint the original number using the print() function and .format() method now use f-string, concatenate the string test_str, integer test_int, and the string test_str again to form a new string and assign it to res and print the final string with the added number using the print() function and .format() method.

Python3




test_str = "Beginner"
 
# initializing number
test_int = 4
 
# printing original string
print("The original string is : {}".format(test_str))
 
# printing number
print("The original number : {}".format(test_int))
 
# Using f-string
res = f"{test_str}{test_int}{test_str}"
 
# printing result
print("The string after adding number is : {}".format(res))


Output

The original string is : Beginner
The original number : 4
The string after adding number is : Beginner4Beginner

Time complexity: O(1)
Auxiliary space: O(1)



Contact Us