python

python programming

Page 1 of 1

6 Replies - 1437 Views - Last Post: 07 December 2009 - 05:11 PM Rate Topic: -----

#1 aus001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-November 09

python

Post icon  Posted 30 November 2009 - 05:56 AM

hi
Does any one know how to print a row of stars like *****
****
***
using python. i can do it in c but not inpython
Is This A Good Question/Topic? 0
  • +

Replies To: python

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: python

Posted 30 November 2009 - 06:02 AM

View Postaus001, on 30 Nov, 2009 - 04:56 AM, said:

hi
Does any one know how to print a row of stars like *****
****
***
using python. i can do it in c but not inpython


If you know how to do it in C, then it's a matter of adapting the syntax to Python.

Here is a hint: http://ibiblio.org/g...d/for-loop.html.
Was This Post Helpful? 0
  • +
  • -

#3 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: python

Posted 05 December 2009 - 09:46 AM

# For loop i from 5 to 0, subtracting 1 each time
for i in range(5, 0, -1):
    print '*' * i


This post has been edited by programble: 05 December 2009 - 09:47 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5749
  • View blog
  • Posts: 12,556
  • Joined: 16-October 07

Re: python

Posted 05 December 2009 - 02:30 PM

print "\n".join([(5-i) * "*" for i in range(3)])


Was This Post Helpful? 0
  • +
  • -

#5 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 194
  • Joined: 21-April 09

Re: python

Posted 06 December 2009 - 09:59 AM

You can also use string formatting:

for _ in range(3):
    print "{0:<5}".format("*")


which will output:
*****
*****
*****

str.format is extremely useful, please look to your resourses to learn it ! (including python.org)
Was This Post Helpful? 0
  • +
  • -

#6 brianmen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 14-June 09

Re: python

Posted 07 December 2009 - 12:17 AM

You could also just use the three quotes style to get a particular format:

print """

*******
*******
*******
*******

"""




That's just if you want to print it once or so.
Was This Post Helpful? 0
  • +
  • -

#7 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: python

Posted 07 December 2009 - 05:11 PM

This has already been answered here!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1