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 certain ways in which this problem can be solved.
Method #1 : Using Type conversion
The simplest way in which this task can be performed is by converting the integer explicitly into string datatype using the basic type conversion and adding it to appropriate position.
# Python3 code to demonstrate working of # Inserting a number in string # Using type conversion # initializing string test_str = "Geeks" # 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)) |
The original string is : Geeks The original number : 4 The string after adding number is : Geeks4Geeks
Method #2 : Using %d operator
This operator can be used to format the string to add the integer. The “d” represents that the datatype to be inserted to string is an integer. This can be changed according to the requirements.
# Python3 code to demonstrate working of # Inserting a number in string # Using % d operator # initializing string test_str = "Geeks" # 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)) |
The original string is : Geeks The original number : 4 The string after adding number is : Geeks4Geeks
Recommended Posts:
- Python | Insert value after each k letters in given list of string
- Python | Insert the string at the beginning of all items in a list
- Python list | insert()
- numpy.insert() in Python
- Python | Insert after every Nth element in a list
- Python | Pandas TimedeltaIndex.insert
- Python | Pandas Index.insert()
- Python | Pandas dataframe.insert()
- MongoDB Python | Insert and Update Data
- MongoDB python | insert(), replace_one(), replace_many()
- Python | Split given list and insert in excel file
- Python program to insert an element into sorted list
- Python | Splitting Text and Number in string
- The most occurring number in a string using Regex in python
- List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



