By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,825 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,825 IT Pros & Developers. It's quick & easy.

Combine output in python after break

P: 4
I want to combine two of my print statement so that it has no gap in between. How do I remove gap after break? This is my code:

Expand|Select|Wrap|Line Numbers
  1. alpha1 = 'a'
  2.     n = 6 
  3.     while alpha1:
  4.        n -= 1
  5.        print "%-*s" % (10, alpha1*n) + "%*s" % (10, alpha1*n)
  6.        if n < 0:
  7.             break
  8.     m = 0
  9.     while alpha1:
  10.        m += 1
  11.        print "%-*s" % (10, alpha1*m) + "%*s" % (10, alpha1*m)
  12.        if m > 4:
  13.             break
The out put will be:

Expand|Select|Wrap|Line Numbers
  1.     aaaaa          aaaaa
  2.     aaaa            aaaa
  3.     aaa              aaa
  4.     aa                aa
  5.     a                  a
  6.  
  7.     a                  a
  8.     aa                aa
  9.     aaa              aaa
  10.     aaaa            aaaa
  11.     aaaaa          aaaaa
  12.  
How do I remove the gap so that the output will be like this?

Expand|Select|Wrap|Line Numbers
  1.     aaaaa          aaaaa
  2.     aaaa            aaaa
  3.     aaa              aaa
  4.     aa                aa
  5.     a                  a
  6.     aa                aa
  7.     aaa              aaa
  8.     aaaa            aaaa
  9.     aaaaa          aaaaa
  10.  
Apr 22 '13 #1

✓ answered by bvdet

You need only change two of the numbers.
Expand|Select|Wrap|Line Numbers
  1. alpha1 = 'a'
  2. n = 6 
  3. while True:
  4.    n -= 1
  5.    print "%-*s" % (10, alpha1*n) + "%*s" % (10, alpha1*n)
  6.    if n < 2:
  7.         break
  8. m = 1
  9. while True:
  10.    m += 1
  11.    print "%-*s" % (10, alpha1*m) + "%*s" % (10, alpha1*m)
  12.    if m > 4:
  13.         break
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> aaaaa          aaaaa
  2. aaaa            aaaa
  3. aaa              aaa
  4. aa                aa
  5. a                  a
  6. aa                aa
  7. aaa              aaa
  8. aaaa            aaaa
  9. aaaaa          aaaaa
  10. >>> 

Share this Question
Share on Google+
2 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
You need only change two of the numbers.
Expand|Select|Wrap|Line Numbers
  1. alpha1 = 'a'
  2. n = 6 
  3. while True:
  4.    n -= 1
  5.    print "%-*s" % (10, alpha1*n) + "%*s" % (10, alpha1*n)
  6.    if n < 2:
  7.         break
  8. m = 1
  9. while True:
  10.    m += 1
  11.    print "%-*s" % (10, alpha1*m) + "%*s" % (10, alpha1*m)
  12.    if m > 4:
  13.         break
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> aaaaa          aaaaa
  2. aaaa            aaaa
  3. aaa              aaa
  4. aa                aa
  5. a                  a
  6. aa                aa
  7. aaa              aaa
  8. aaaa            aaaa
  9. aaaaa          aaaaa
  10. >>> 
Apr 22 '13 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Here's a version I like better:
Expand|Select|Wrap|Line Numbers
  1. letter = "a"
  2. no_letters = 5
  3. output = []
  4. for i in range(no_letters, 1, -1):
  5.     output.append("%-*s%*s" % (10, letter*i, 10, letter*i))
  6. for i in range(1, no_letters+1):
  7.     output.append("%-*s%*s" % (10, letter*i, 10, letter*i))
  8. print "\n".join(output)
Apr 22 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.