Reputation: 719
I have written the python code as below:
magicNumber = 25
for n in range(100):
if n is magicNumber:
print(n, " is the magic number")
break
else:
print(n)
The last line of output is showing in the format as below:
(25, ' is the magic number')
Please let me know what can i do to have the last line
of the output
as:
25 is the magic number
Upvotes: 2
Views: 586
Reputation: 3170
for python 2.x - print acts as a command just remove the brackets and it will work as expected.
print n, " is the magic number"
for python 3.x - print acts as a function; so below is fine.
print(n, " is the magic number")
There are some other methods also as suggested by the user abccd.
Upvotes: 1
Reputation: 172
You're running the code in python 2. That explains the braces being printed. Run in python 3 it'll work as you expect. Or if you still prefer python 2,then just remove the braces and put
print n,' is the magicnumber'
Upvotes: 1
Reputation: 33704
There's many ways you can accomplish this, since you're using python 2, there isn't a default print function. So you will have to import it, one way will be to add this on the top of your code.
from __future__ import print_function
Other ways include using string formatting such as:
print "%s is the magic number" % n
And
print "{0} is the magic number".format(n)
Or you can just easily remove the brackets and it will all be the same.
print n, "is the magic number"
Upvotes: 3