Print statement not printing as it suppose to

Tim Delaney timothy.c.delaney at gmail.com
Fri Sep 20 18:11:26 EDT 2013


On 21 September 2013 07:57, Sam <anasdahany at gmail.com> wrote:

> hi everybody i am just starting to learn python, i was writing a simple
> i/o program but my print statement is acting weird. here is my code i want
> to know why it prints this way. thank you
>
> print("\nThe total amount required is ", total )
>
>
> OUTPUT
>
> ('\nThe total amount required is ', 3534)
>
> ===> the problem is obviously on the last print statement that is supposed
> to print the outut
>

Check your version of Python. The output you have given says that you're
using a Python 2 version, but the print syntax you're using is for Python
3. Unfortunately, you've hit one of the edge cases where they produce
different output.

As a general rule, either use % formatting or format()to produce a single
string to print, rather than relying on print to output them correctly for
you (or using string concatenation). Since you're only just starting you
won't have got to them yet - the simplest way to to it is to just insert
the string representation of all parameters. The above done using %
formatting would be:

print("\nThe total amount required is  %s" % (total,))

which will produce the same output on both Python 2 and Python 3. Note the
double space before %s - that matches your print statement (there would be
soft-space inserted in your print statement, which is another reason not to
rely on print for anything other than single strings). If you didn't want
that extra space, it's easy to delete.

Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130921/d091b04d/attachment.html>


More information about the Python-list mailing list