Using print function without parentheses works with older versions of Python but is no longer supported on Python3, so you have to put the arguments inside parentheses. However, there are workarounds, as mentioned in the answers to this question. Since the support for Python2 has ended in Jan 1st 2020, the answer has been modified to be compatible with Python3.
You could do any of these [and there may be other ways]:
[1] print["First number is {} and second number is {}".format[first, second]]
[1b] print["First number is {first} and number is {second}".format[first=first, second=second]]
or
[2] print['First number is', first, 'second number is', second]
[Note: A space will be automatically added afterwards when separated from a comma]
or
[3] print['First number %d and second number is %d' % [first, second]]
or
[4] print['First number is ' + str[first] + ' second number is' + str[second]]
Using format[] [1/1b] is preferred where available.
In Python, we normally perform string concatenation using the So what would happen if we have a string and an int on both sides of the operand? Since Python is a dynamically typed language, we would not
face any error during compilation, but rather, we get a Runtime Error. [More specifically, a TypeError exception is raised] The below snippet proves this:+
operator. The +
operator, however as we know, is also used to add integers or floating-point numbers.a = "Hello, I am in grade "
b = 12
print[a + b]
Output
Traceback [most recent call last]: File "concat.py", line 5, in print[a + b] TypeError: can only concatenate str [not "int"] to str
So, since we cannot directly concatenate an integer with a string, we need to manipulate the operands so that they can be concatenated. There are multiple ways of doing this.
1. Using str[]
We can convert the integer to a string,
via the str[]
function. Now, the new string can now be concatenated with the other string to give the Output;
Output
This is the most common way to convert an integer to a string.
But, we can use other methods as well.
2. Using format[]
a = "Hello, I am in grade " b = 12 print["{}{}".format[a, b]]
The output remains the same as before.
3. Using ‘%’ format specifier
a = "Hello, I am in grade " b = 12 print["%s%s" % [a, b]]
While we can specify that both a
and b
are strings, we can also use C-style
format specifiers [%d
, %s
] to concatenate an integer with a string.
The output remains the same for the above code.
4. Using f-strings
We can use Python f-strings on Python 3.6 or above to concatenate an integer with a string.
a = "Hello, I am in grade " b = 12 print[f"{a}{b}"]
5. Printing the string using print[]
If we want to directly print the concatenated string, we can use print[]
to do the concatenation for us.
a = "Hello, I am in grade " b = 12 print[a, b, sep=""]
We join a
and b
using a null
string separator [sep
], since the default separator for print[]
is a space [” “].
Conclusion
In this article, we learned how to concatenate an integer to a string using various methods.
References
- StackOverflow Question on Concatenation of string and int
- JournalDev article on Concatenation of string and int
Use comma “,” to separate strings and variables while printing int and string in the same line in Python or convert the int to string. There are many ways to print int and string in one line, some methods are:- Method 1 Method 2 When adding strings, they
concatenate. Method 3 Method 4 the format [Python 2.6 and newer] method of strings is probably the standard way: Simple
example code.print['String', int_vlaue]
print['string' + str[a]]
print['String %d %s' % [a, b]]
print['String {} {}'.format[a, b]]
Example how to print variable and string on the same line in Python
s = "Hello"
i = 10
print[s, i]
print[s + str[i]]
print["String %d %s" % [10, 20]]
print["String {}".format[i]]
Output:
Do comment if you have any doubts and suggestions on this Python int string topic
Note: IDE: PyCharm 2021.3.3 [Community Edition]
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.