473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Combine output in python after break

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. >>> 

2 1517
bvdet
2,851 Expert Mod 2GB
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
2,851 Expert Mod 2GB
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

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

Similar topics

1
by: Krzysztof Pa¼ | last post by:
Hi, I want to make simple client in phyton, which would be able to communicate with Java server using SSL sockets. There is the Java clients, which is doing this - so I'm pretty sure, that Java...
7
by: Hansan | last post by:
Hi all, I hope you have time to help me out a little. My problem is that I want to combine some python code I have made with some html templates, I found a tutorial at dev shed:...
11
by: Grant Edwards | last post by:
I've read over and over that Python leaves floating point issues up to the underlying platform. This seems to be largely true, but not always. My underlying platform (IA32 Linux) correctly...
3
by: Paul Watson | last post by:
It is clear that just using 'print' with variable names is relatively uncontrollable. However, I thought that using a format string would reign the problem in and give the desired output. Must...
0
by: nojhan | last post by:
I'm trying to embbed the python interpreter as a class attribute, initializing it in the constructor, and finalizing in destructor. The code is rather simple: // base_path is an attribute of...
6
by: Wijaya Edward | last post by:
Can we make loops control in Python? What I mean is that whether we can control which loops to exit/skip at the given scope. For example in Perl we can do something like: OUT: foreach my $s1...
10
by: notejam | last post by:
I am trying to get started with a interactive version of Python for windows and need some help. I have played with the tutorial, and now want to write a program. In basic language, I could write...
4
by: Jia Lu | last post by:
Hi all I'm trying to parsing html with re module. html = """ <TABLE BORDER=1 cellspacing=0 cellpadding=2> <TR> <TH nowrap>DATA1</TH><TH nowrap>DATA2</HT><TH nowrap>DATA3</...
9
by: dominiquevalentine | last post by:
Hello, I'm a teen trying to do my part in improving the world, and me and my pal came up with some concepts to improve the transportation system. I have googled up and down for examples of using...
2
by: Dale | last post by:
I am using a simple python webserver (see code below) to serve up python scripts located in my cgi-bin directory. import BaseHTTPServer import CGIHTTPServer class...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.