mrm
mrm

Reputation: 69

How do I print strings in a list side-by-side?

I have an input that is a list of strings, and I'm not sure how to print every string in the list side-by-side, and add hyphens underneath each string.

Example input:

['13 + 11', '12 - 4', '36 + 18']

Desired output:

  13     12      36

+ 11    - 4    + 18

What I've tried:

I Tried using a for loop to loop over the strings in the input list, and splitting them up on a space:

for i in problems:
    j = i.split(' ')
    print(j)

Output from trying this:

['13', '+', '11']
['12', '-', '4']
['36', '+', '18']

I've tried combining split and join methods to change the output, but I'm missing a few key pieces of knowledge. I'm lost as to how to go about printing the strings side-by-side. I have a feeling it's something to do with including a newline character, but beyond that I’m having trouble.

Upvotes: 0

Views: 806

Answers (3)

twiddler
twiddler

Reputation: 588

to get your desired output, I tried this:

join_spaces = '    '
mylist = ['13 + 11', '12 - 4', '36 + 18']
top = []
bottom = []

for each in mylist:
    number1, number2 = each.split(' ', 1)
    bottom.append(number2)
    top.append(number1)

for t in range(len(top)):
    length = " " * (len(bottom[t]) - len(top[t]))
    top[t] = length + top[t]

print(join_spaces.join(top))
print(join_spaces.join(bottom))

not the cleanest method (written quickly)

Explanation: for each value in the list, use split(' ', 1) to split at the first occurrence of a space; this would be the first number (13, 12, 36). Then add this to the top list, and append the rest ("+ 11", "- 4", "+ 18") to the bottom list. Then, for each value in the list top, subtract the length of the bottom ("+ 11", "- 4", "+ 18") from the top (13, 12, 36), and multiply that number by a space. Then, add the spaces to the beginning of every value in the top list, and print out the lists as strings (join)

outputs:

  13     12      36
+ 11    - 4    + 18

Upvotes: 1

Alan
Alan

Reputation: 976

Assuming your expressions are always of the form "a 'operation' b":

problems = ['13 + 11', '12 - 4', '36 + 18']

split_problems = []
for i in problems:
    j = i.split(' ')
    split_problems.append(j)

result = ""

for i in range(len(split_problems)):
    result += split_problems[i][0] + " "

result += '\n'

for i in range(len(split_problems)):
    result += split_problems[i][1] + "" + split_problems[i][2] + " "

print(result)

Solution explained:

First split the whole thing into parts just like you did, resulting in a array like [['13', '+', '11'], ['12', '-', '4'], ['36', '+', '18']]

Then loop over the outer elements and print every first entry, or in this case append them to a result string. Then loop over the outer elements again and print every 2nd and 3rd element per iteration or append them to a result string.

Upvotes: 1

Tomerikoo
Tomerikoo

Reputation: 19414

As you discovered, using split will get you something like this:

>>> l = ['13 + 11', '12 - 4', '36 + 18']
>>> list(map(str.split, l))
[['13', '+', '11'], ['12', '-', '4'], ['36', '+', '18']]

Now you can use zip() to take index-matching elements:

>>> list(zip(*map(str.split, l)))
[('13', '12', '36'), ('+', '-', '+'), ('11', '4', '18')]

And now just print it in a loop:

>>> for row in zip(*map(str.split, l)):
...     print(*row, sep='\t')
    
13  12  36
+   -   +
11  4   18

To get something closer to what you asked, you can split only once (using the maxsplit argument) and format the output a bit:

l = ['13 + 11', '12 - 4', '36 + 18']
for row in zip(*[s.split(' ', 1) for s in l]):
    print(*[f"{num:>6}" for num in row])

Will give:

    13     12     36
  + 11    - 4   + 18

Upvotes: 1

Related Questions