6 Replies - 742 Views - Last Post: 24 February 2012 - 08:47 PM Rate Topic: -----

#1 bita  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 190
  • Joined: 21-April 09

Printing a parameter

Posted 20 February 2012 - 04:57 AM

Hi guys,

I have a problem in printing parameters, here is my code:

length = 5
breadth = 2
area = length * breadth
print('Area is', area)
print('Perimeter is', 2 * (length + breadth))



I expect to have this out put:

Area is 10
Perimeter is 14

But what I see a an out put is this:

('Area is', 10)
('Perimeter is', 14)

Is there anybody who can help me with this printing problem?

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Printing a parameter

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Printing a parameter

Posted 20 February 2012 - 05:11 AM

What you need is string formatting:

length = 5
width = 2
area = length * widith
perimeter = 2 * (length + width)
print("Area is %.2f" % (area))
print("Perimeter is %.2f" % (perimeter))


The % operator is overloaded in Python. In addition to it's arithmatic use, it is use to format strings. The characters that follow the % operator (before a space) describe what it is we want to print. In this case we have two things of note: 1) f says that we want to print a floating-point number. While area and perimeter are both integers in this instance, they could easily be floats. 2) .2 tells Python that we want to print our floating-point number to two decimal places. It's also possible to add a number to the left of the decimal point, but that is outside the scope of this post.

Check out 6.1.3 -- Format String Syntax (more specifically, 6.1.3.2 -- Format Examples) for more information.
Was This Post Helpful? 2
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Printing a parameter

Posted 20 February 2012 - 05:58 AM

While Brewers explanation is fully correct, it doesn't say why it was printed like that: the reason being that you are using Python 3 print syntax run under Python 2.

Your print statement prints the tuple ("Area", 10):
print("Area", length * width)


Which would print what you were expecting under Python 3.x,

The equivalent statement run under Python 2, would be:

print "Area", length * width


Without the brackets. String formatting isn't necessary in this example but it's always nice :)

This post has been edited by Simown: 22 February 2012 - 05:49 AM

Was This Post Helpful? 1
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Printing a parameter

Posted 23 February 2012 - 11:01 PM

Sorry to say, I wouldn't use Brewer's approach. That style of String formatting was deprecated as of Python 2.6, which means it won't be supported in later versions.

Most of Python DOCS seems to be down tongiht, but I did find this link confirming it. It's under the section "Changes Already Present In Python 2.6".

This post has been edited by atraub: 23 February 2012 - 11:06 PM

Was This Post Helpful? 1
  • +
  • -

#5 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Printing a parameter

Posted 24 February 2012 - 11:42 AM

View Postatraub, on 24 February 2012 - 02:31 AM, said:

Sorry to say, I wouldn't use Brewer's approach. That style of String formatting was deprecated as of Python 2.6, which means it won't be supported in later versions.

Most of Python DOCS seems to be down tongiht, but I did find this link confirming it. It's under the section "Changes Already Present In Python 2.6".


Hmm, that is interesting. I'll admit that I'm not terribly familiar with Python 3. +1

The new formatting syntax looks incredibly un-Pythonic. They should have stuck with the modulo syntax.
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Printing a parameter

Posted 24 February 2012 - 05:25 PM

The modulo syntax was very C lol :-P

This post has been edited by atraub: 24 February 2012 - 05:26 PM

Was This Post Helpful? 0
  • +
  • -

#7 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 17
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Re: Printing a parameter

Posted 24 February 2012 - 08:47 PM

I have a similar question about output formatting. I currently have a matrix of outputs (as a list of lists), which I'd like to print in columns, but the length of some of the elements are variable. Currently, I just have something ugly (and which I just learned was deprecated...):
print "%s\t%d\t%s\t%.2fC\t%s"%(primerDict[str(primer[0])], primer[1], primer[2], primer[3], primer[4])


For example, a few lines of output might be:
p8    2232    GTGATGCGGAGAGATGGGTAATGAAGATGGACAAAAAGACTATAGTTTGG    67.87C    True
p9    2234    GATGCGGAGAGATGGGTAATGAAGATGGACAAAAAGA    65.21C    True


There has to be a way to buffer the differences in length with white space and maybe even wrap the longer sequences, but I've yet to figure it out. I have found this, but I can't really make heads or tails of the formatting gibberish...

Any thoughts?

Edit: typos...

This post has been edited by SPlutard: 24 February 2012 - 08:49 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1