Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54514

Re: Print statement not printing as it suppose to

References <05bbf1a3-6480-48ee-8984-2482b90c79c0@googlegroups.com>
Date 2013-09-21 08:11 +1000
Subject Re: Print statement not printing as it suppose to
From Tim Delaney <timothy.c.delaney@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.202.1379715089.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 21 September 2013 07:57, Sam <anasdahany@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Print statement not printing as it suppose to Sam <anasdahany@gmail.com> - 2013-09-20 14:57 -0700
  Re: Print statement not printing as it suppose to Tim Delaney <timothy.c.delaney@gmail.com> - 2013-09-21 08:11 +1000
  Re: Print statement not printing as it suppose to Emiliano Carlos de Moraes Firmino <emiliano.firmino@gmail.com> - 2013-09-20 18:11 -0400
  Re: Print statement not printing as it suppose to John Gordon <gordon@panix.com> - 2013-09-20 22:40 +0000
  Re: Print statement not printing as it suppose to Chris Angelico <rosuav@gmail.com> - 2013-09-21 12:50 +1000
  Re: Print statement not printing as it suppose to Dave Angel <davea@davea.name> - 2013-09-21 04:40 +0000

csiph-web