0

Does any one know how to print this pattern using python?

0000000001 0000000002 0000000003 0000000004 0000000005 0000000006 0000000007 0000000008 0000000009 0000000010 0000000011

I am a beginner here..Pls help..

1
  • Beginner or not...c'mon. Could you please make some effort towards solving it? How would you approach it on paper? Commented Jan 8, 2015 at 4:36

2 Answers 2

2

Use zfill(n) function of str class to fill zeros on the left of the string to make it of some length n.

>>> for i in range(1,15):
...    print str(i).zfill(10),
... 
0000000001 0000000002 0000000003 0000000004 0000000005 0000000006 0000000007 0000000008 0000000009 0000000010 0000000011 0000000012 0000000013 0000000014
Sign up to request clarification or add additional context in comments.

Comments

2

you can also use rjust:

>>> for x in range(20):
...     print str(x).rjust(10,'0'),
... 
0000000000 0000000001 0000000002 0000000003 0000000004 0000000005 0000000006 0000000007 0000000008 0000000009 0000000010 0000000011 0000000012 0000000013 0000000014 0000000015 0000000016 0000000017 0000000018 0000000019

using format:

>>> for x in range(20):
...     print "{:0>10}".format(x),
... 
0000000000 0000000001 0000000002 0000000003 0000000004 0000000005 0000000006 0000000007 0000000008 0000000009 0000000010 0000000011 0000000012 0000000013 0000000014 0000000015 0000000016 0000000017 0000000018 0000000019

Comments

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.