0

I am currently learning python and would like to know how these two print statements are different? I mean both perform the same action but only differ in the syntax. Are there any other differences?

a = 5
b = 'hi'

print "The number is", a, " and the text is", b

print "The number is %d and the text is %s" %(a, b)
3
  • 2
    First one won't even run. Commented Feb 7, 2014 at 16:44
  • @AshwiniChaudhary It's just missing a comma. Commented Feb 7, 2014 at 16:45
  • 4
    Consider print("The number is {} and the text is {}".format(a, b)) Commented Feb 7, 2014 at 16:46

1 Answer 1

2

Well, the second one will fail if the variable a is not a number.

>>> a='hi'
>>> b='hi'
>>> print "The number is %d and the text is %s" %(a, b)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-aa94a92667a1> in <module>()
----> 1 print "The number is %d and the text is %s" %(a, b)

TypeError: %d format: a number is required, not str

If a is always a number, they would behave very much alike, except the %d in the format forces it to be an integer, so if you have:

>>> a=1.2
>>> b='hi'
>>> print "The number is %d and the text is %s" %(a, b)
The number is 1 and the text is hi

You can see that it converts the number 1.2 to an integer 1.

As per the comments, another option is to use the format function, that behaves similar to your first option but using a format string:

>>> a=1.2
>>> b='hi'
>>> print "The number is {} and the text is {}".format(a, b)
The number is 1.2 and the text is hi

It also allows to use named arguments:

>>> print "The number is {number} and the text is {text}".format(number=a, text=b)
The number is 1.2 and the text is hi
Sign up to request clarification or add additional context in comments.

8 Comments

I'm deleting my answer because yours in much better, but for the sake of completeness you should consider providing a description of the format() function
@wnnmaw Okay, I'll add a note. If you want, you can suggest an edit too, and I'll apply. =)
Just a warning, "the first one" has changed since you answered (the question was edited). Now in the first one a can't be a number (repr() is no longer called), and in the second one it must be!
@SteveJessop ops, someone other than the OP changed the question inadvertently -- let me change it back.
I'll throw my name in the hat for suggesting use of the str.format function instead of string concatenation or interpolation using %s. str.format is very robust, and makes debugging a breeze when you know "Hey, something's going wrong here, let's just do "a is {a} and b is {b} and c is {c} and foo is {foo} and bar is {bar}".format(**locals()) (though NEVER use locals() in production code)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.