1

I have two strings , the length of which can vary based on input. I want to format them aligning them to middle and filling up the rest of the space with ' '. Each string starting adn ending with ^^ .

Case1:

String1 = Longer String
String2 = Short

Output required:

^^   Longer String   ^^
^^       Short       ^^

Case2:

String1 = Equal String1
String2 = Equal String2

Output required:

^^       Equal 1      ^^
^^       Equal 2      ^^

Case3:

String1 =  Short
String2 = Longer String

Output required:

^^       Short       ^^
^^   Longer String   ^^

Across all three outputs the legth has been kept constant , so that uniformity is maintained.

My initial thought is that this will involve checking lengths of the two strings in the following format

if len(String1) > len(String2):
  #Do something
else:
  #Do something else
1
  • What's wrong with str.center()? Commented Jul 5, 2013 at 0:23

3 Answers 3

2

Simply use str.center:

assert '^^' + 'Longer String'.center(19) + '^^' == '^^   Longer String   ^^'
assert '^^' + 'Short'.center(19) + '^^'         == '^^       Short       ^^'
Sign up to request clarification or add additional context in comments.

3 Comments

Mate the strings can vary . Doesn't string.center(19) hardcode the length value being centered around?
@misguided Yes, but what is the formula for the width you want to have? All of your examples use width 19, so this code solves the original question. Can you provide an example of an output with a length != 19, and answer why the width is 19 both for Equal and Longer String?
Fair enough. My examples might have misled. I agree with your solution though. Very helpful . Thanks.
0

If you want to reference just setting the centering with respect to two strings:

cases=[
    ('Longer String','Short'),
    ('Equal 1','Equal 2'),
    ('Short','Longer String'),
    ]

for s1,s2 in cases:
    w=len(max([s1,s2],key=len))+6
    print '^^{:^{w}}^^'.format(s1,w=w)
    print '^^{:^{w}}^^'.format(s2,w=w)
    print

Prints:

^^   Longer String   ^^
^^       Short       ^^

^^   Equal 1   ^^
^^   Equal 2   ^^

^^       Short       ^^
^^   Longer String   ^^

Or, if you want to test the width of more strings, you can do this:

cases=[
    ('Longer String','Short'),
    ('Equal 1','Equal 2'),
    ('Short','Longer String'),
]

w=max(len(s) for t in cases for s in t)+6
for s1,s2 in cases:
    print '^^{:^{w}}^^'.format(s1,w=w)
    print '^^{:^{w}}^^'.format(s2,w=w)
    print 

prints:

^^   Longer String   ^^
^^       Short       ^^

^^      Equal 1      ^^
^^      Equal 2      ^^

^^       Short       ^^
^^   Longer String   ^^

Comments

0

I ended up using the following code, which worked for me . 19 can be replaced by any number based on how long you want your string to be formatted to .

print "^^",'{:^19}'.format(String1),"^^"
print "^^",'{:^19}'.format(String2),"^^"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.