9

I am using underscores to represent the length of a unknown word. How can I print just the underscores without the brackets that represent the list?

Basically, if I have a list of the form ['_', '_', '_', '_'], I want to print the underscores without printing them in list syntax as "_ _ _ _"

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
user1718826
  • 347
  • 5
  • 7
  • 11
  • Does this answer your question? [How do I convert a list into a string with spaces in Python?](https://stackoverflow.com/questions/12309976/how-do-i-convert-a-list-into-a-string-with-spaces-in-python) – mkrieger1 May 28 '22 at 17:11

3 Answers3

38

Does this work for you

>>> my_dashes = ['_', '_', '_', '_']
>>> print ''.join(my_dashes)
____
>>> print ' '.join(my_dashes)
_ _ _ _
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
1
In [1]: my_dashes = ['_', '_', '_', '_']

In [2]: str(my_dashes).translate(None, '[],\'')
Out[2]: '_ _ _ _'

Add an extra space in the deletechars string to put the dashes together.

chimpsarehungry
  • 1,775
  • 2
  • 17
  • 28
1

The previously-mentioned join solution (like in following line) is a reasonable solution:

print ''.join(['_', '_', '_', '_'])
____

But also note you can use reduce() to do the job:

print reduce(lambda x,y: x+y, ['_', '_', '_', '_'])
____

After import operator you can say:

print reduce(operator.add, ['_', '_', '_', '_'])
____
James Waldby - jwpat7
  • 8,593
  • 2
  • 22
  • 37