Home > Back-end >  How to center a list of strings Without using the center method
How to center a list of strings Without using the center method

Time:09-21

I've been trying to figure this out for a while and completely stuck. I've been coding in R for years and have been trying to learn Python recently.

I'm suppose to create a function in Python that takes in a list of strings and returns a new list of strings with each string padded with blanks so that when its printed it would be centered.

As an example a given list:

a = ["I LIKE","Dogs","Black Dogs, Green Dogs","Bad Dogs, Mean Dogs","All kinds of Dogs","I like Dogs", "Dogs"]

Should return as:

       I LIKE
        Dogs
Black Dogs, Green Dogs
          .
          .
          etc

So far I have this and it's obviously not working correctly. I also don't know if I'm overthinking this by having multiple for loops?

def center_me(x):
    num = 0
    long_num = 0
    for i in a:
      if len(i) > long_num:
          long_num = len(i)
          res = i
    ssv = len(res)

    for i in a:
      max = len(i)
      if len(a) > num: 
        new = ssv - max
        print(" " * new, i, " " * new)

    # test it
    def simple_test():
        a = ["I LIKE","Dogs","Black Dogs, Green Dogs","Bad Dogs, Mean Dogs","All kinds of Dogs","I like Dogs", "Dogs"]
        for e in center_me(a):
            print(e)

CodePudding user response:

Try:

a = [
    "I LIKE",
    "Dogs",
    "Black Dogs, Green Dogs",
    "Bad Dogs, Mean Dogs",
    "All kinds of Dogs",
    "I like Dogs",
    "Dogs",
]

max_len = len(max(a, key=len))

for s in a:
    print(" " * (max_len // 2 - len(s) // 2)   s)

Prints:

        I LIKE
         Dogs
Black Dogs, Green Dogs
  Bad Dogs, Mean Dogs
   All kinds of Dogs
      I like Dogs
         Dogs

CodePudding user response:

Perhaps you wanted to write the code yourself, but if you just want something that works, in plain Python:

a = [
    "I LIKE",
    "Dogs",
    "Black Dogs, Green Dogs",
    "Bad Dogs, Mean Dogs",
    "All kinds of Dogs",
    "I like Dogs",
    "Dogs",
]
for line in a:
    print(f'{line:^{len(max(a, key=len))}s}')

This uses an f-string of the type:

print(f'{x:^10s}')

Which prints x centered in 10 positions.

If you do want to do the work yourself, that many loops is definitely overthinking it.

Consider something like:

def center_these(xs):
    m = len(max(a, key=len))
    for x in xs:
        space = m - len(x)
        print(' ' * (space // 2)   x   ' ' * (space // 2   space % 2))


center_these(a)

This function works by computing the maximum length in the list, and then for each string in the list, printing a number of spaces half the difference between its length and the maximum (rounded down), then the string itself, and then the same number of spaces again, but with the remainder added.

For example: if m is 10, and you're printing short, then the length of short is 5, (8 - 5) // 2 is 2, and (8 - 5) % 2 is 1, so what's printed is ' ' * 2 'short' ' ' * (2 1)

If you don't need to actually print the same number of characters on each line and are happy for the line to end early, you can also just:

def center_these(xs):
    m = len(max(a, key=len))
    for x in xs:
        print(' ' * ((m - len(x)) // 2)   x)

Which is similar to what @AndreyKesely suggested.

Since you're just learning Python, the key takeaway here is that Python allows you to use arithmetic operations quite flexibly:

# this is 'aaa'
'a' * 3

# this is 'niiice'
'n'   'i' * 3   'ce'

An of course, you need to be aware of all the basic math operators like // (integer division) and % (modulo, i.e. integer division remainder).

  • Related