Do you want to concatenate String and Integer as String in Python or do you want to Print both at once? Learn how to concatenate String and Integer in Different Ways.
Since in Python you cannot use the ‘+’ operator directly on string and integer to get both of them to concatenate to a single string unless you are typecasting the integer to string.
1. By Typecasting Integer to String
The best and fastest way to concatenate any string with an integer can be done using the typecasting of an integer value. If you are not aware of what is typecasting then in simple words you are converting the existing integer value to a string.
Let us see in the below example code the usage of typecasting of Integer in Python.
#Initializing a String giveString = "My Year of Birth is " #Initializing the Integer birthYear = 1984 #Concatenating the birth Year to String toPrint = giveString + str(birthYear) #Printing the Sentence print(toPrint)
Output:
My Year of Birth is 1984
Let us take another example in which you want to concatenate a few numbers while looping to a range of numbers.
#Initializing a String giveString = "The Numbers are " for i in range(0, 10): giveString += str(i) #Printing the Sentence print(giveString)
Output:
The Numbers are 0123456789
So, in the above example, I have typecasted integer every time from int to string and then used the ‘+’ operator to concatenate them.
2. Using Format Method In Python
In the String Class of Python, there is a format[1] method that can be used to concatenate the string. This method as well typecast any data type to string and then it can be easily added to the existing string.
Let us see in the below example code the usage of the Format Method of String Class in Python.
#Initializing a String giveString = "The Numbers are " #Using Format Method to Concatenate the String for i in range(0, 10): giveString = giveString + "{}".format(i) #Printing the Sentence print(giveString)
Output:
The Numbers are 0123456789
As you can see we need to have “{}” that acts as a syntax for the format function and this lets the format method know where to insert the input argument after typecasting it to string.
Wrap Up
I hope with help of the above method you might be able to concatenate the string and integer in Python. I have discussed the two most important methods that are widely used in the software industry and you can use any one of them based upon your requirement.
If you want to only print the values then the format method is your best option. But if you want to continue doing some operation on the concatenated string then typecasting and using the ‘+’ operator will be a great option to use.
Let me know in the comment section if you know any better method than the one discussed above I will be happy to add it here.
If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.
Further Read:
- Code Example: Convert a String to DateTime in Python
- How Do I Sort A Dictionary By Value In Python
- Code Example: How To Add Numbers In A List Python
- Python – How To Delete All Files In A Directory
- How To Remove Non-alphanumeric Characters From String In Python
- How To Read Image Name From Folder in Python
- How To Multiply Without Using * In Python
- How To Add or Append Values To a Set In Python
- 4 Ways To List All Subdirectories in a Directory – Python
- Dict To list – Convert A Dictionary to a List in Python
- Discord.py Bot Add Reaction To a Message Python