0

Python3 has the super string.format printing:

'{} {}'.format('one', 'two')

If my strings are in an array, one way would be to type them out:

a = ['one','two']
'{} {}'.format(a[0],a[1])

But how can I print from an array, instead of having to type out each element?

For example, broken code:

a = ['one','two']
'{} {}'.format(a)

Gives me an expected error: IndexError: tuple index out of range

Of course, playing with ','.join(a) won't help, because it gives one string rather than 2.

(Or is there a way to do this better with f-strings?)


And for full-disclosure, I'm using a raw-string because it has some geometrical significance, and my real code looks like this:

hex_string = r'''
            _____
           /     \
          /       \
    ,----(    {}    )----.
   /      \       /      \
  /   {}    \_____/   {}    \
  \        /     \        /
   \      /       \      /
    )----(    {}    )----(
   /      \       /      \
  /        \_____/        \
  \   {}    /     \   {}    /
   \      /       \      /
    `----(    {}    )----'
          \       /
           \_____/
'''

letters = list('1234567')

print(hex_string.format(letters[0], letters[1], letters[2], letters[3], letters[4], letters[5], letters[6]))
philshem
  • 24,761
  • 8
  • 61
  • 127
  • if anyone is curious, it's for an open source python port of the New York Times' puzzle game "Spelling Bee": https://github.com/philshem/open-spelling-bee – philshem May 11 '19 at 16:57
  • I'm not sure any of the usual string formatting schemes will help here since you need the output of each formatted item to leave the surrounding data untouched. – quamrana May 11 '19 at 17:00
  • @quamrana yes, I had to add one space character each time I used `{}` because the brackets are replaced by one character – philshem May 11 '19 at 17:01
  • Your example above uses `'one'` and `'two'`. What about when `'three'` turns up? – quamrana May 11 '19 at 17:02
  • yes, and actually, my question isn't specific enough, because I always want `letters[0]` in the center hexagon, which is the 4th `{}` – philshem May 11 '19 at 17:04
  • You may need to organise this problem into a series of questions you can ask on this site. – quamrana May 11 '19 at 17:07
  • yes, good idea. thanks! https://stackoverflow.com/q/56092622/2327328 – philshem May 11 '19 at 17:17

3 Answers3

6

Use unpacking to expand the array during the function call.

print(hex_string.format(*letters))

Output:

            _____
           /     \
          /       \
    ,----(    1    )----.
   /      \       /      \
  /   2    \_____/   3    \
  \        /     \        /
   \      /       \      /
    )----(    4    )----(
   /      \       /      \
  /        \_____/        \
  \   5    /     \   6    /
   \      /       \      /
    `----(    7    )----'
          \       /
           \_____/

rdas
  • 20,604
  • 6
  • 33
  • 46
2

Try unpacking the elements of the list using * as following. For example, printing would look like

print ('{} {}'.format(*a))
# one two
Sheldore
  • 37,862
  • 7
  • 57
  • 71
1

Use the * notation for lists:

print(hex_string.format(*letters))
T Burgis
  • 1,395
  • 7
  • 9