me24hour
me24hour

Reputation: 719

Print output not showing the correct format

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

Answers (3)

Abhishake Gupta
Abhishake Gupta

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

Shyam Prasad
Shyam Prasad

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

Taku
Taku

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

Related Questions