0

I'm having trouble formatting the strings below. Trying to keep the message centered with the borders, but having a hard time executing it. Below is what output I want and below that is my code I have so far. Note: I need to have a space on the left and right columns of the longest line and I need to utilize the split() func.

+---------------+
|     STOP!     |
| DANGER AHEAD  |
| Drive Safely! |
+---------------+

def border_sign(note):
    letter_count = 0
    for i in note:
        letter_count += 1
        return "+-------+\n| {} |\n+-------+\n".format(note)
 border_sign("STOP\nDANGER AHEAD\nDrive safely!")
6
  • 3
    Is this how the code is set up exactly in your editor? Because you are returning inside your loop. Commented Oct 10, 2016 at 22:18
  • 1
    And it's iterating over the characters, too. Commented Oct 10, 2016 at 22:19
  • 1
    And you're formatting the entire note, and using a counter that doesn't do anything, and not doing anything with i... Commented Oct 10, 2016 at 22:22
  • Why are you using a loop? In your example, you never make use of it. Commented Oct 10, 2016 at 22:22
  • I assumed I needed to count the largest string and then add a space for the column, but realized it was wrong. Commented Oct 10, 2016 at 22:34

3 Answers 3

2

Split the string into separate lines, then center each line. Remember to either print each line immediately within the function, build up a list, or yield each line rather than returning after the first line. You can also determine and use a calculated width rather than a static value.

def border_sign(note):
    note = note.splitlines()
    m = len(max(note, key=len))
    yield ('-'*m).join('++')
    for line in note:
        yield ("|{:^%d}|" % m).format(line)
    yield ('-'*m).join('++')

print(*border_sign("STOP\nDANGER AHEAD\nDrive safely!"), sep='\n')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation on the split. If I was using other test functions, how would I go about yielding the correct amount of '+'s and '-'s?
@AlanWakke - See the edit. The +s don't change (it's simply one in each corner), but you can calculate the necessary space and - characters based on the maximum width.
1

Quick sample to consider, not production ready code:

def border_sign(note):
    rows = note.splitlines()
    mlen = len(max(rows, key=len))
    print "+" + "-" * (2+mlen) + "+"
    for row in rows:
        print ("| {:^"+str(mlen)+"} |").format(row)
    print "+" + "-" * (2+mlen) + "+"

Comments

0
def border_sign(x):
    splitted = x.splitlines()
    M = max(map(len,splitted))
    horiz = '+-%s-+' % (M*'-')
    patiz = '| {0: ^%d} |' %  M
    print(horiz,*map(patiz.format,splitted), horiz, sep='\n')

border_sign("STOP\nDANGER AHEAD\nDrive safely!")

patiz.format is a function, then can be used in map

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.