0

How to print an array without brackets but with the quotation marks on it?

I have:

a = ['1','2','3','4','5']
' '.join(map(str, a))

Result I'm receiving:

1, 2, 3, 4, 5

Expected Result (wanted):

'1','2','3','4','5'

(with the commas and the quotation marks)

2
  • 1
    But it doesn't even give your result though? It gives '1 2 3 4 5' Commented Feb 21, 2019 at 14:50
  • print(', '.join(repr(i) for i in a)) will do proper quoting even with quotation marks in the string. Commented Feb 21, 2019 at 14:56

5 Answers 5

6

Do this:

a = ['1','2','3','4','5']
print(str(a).strip('[]'))
Sign up to request clarification or add additional context in comments.

Comments

4

That's because when you do ' '.join(...), you're getting one big string instead of a list of them. If you want to keep the quotes, you'll need to add them back in yourself:

>>> print(', '.join("'{}'".format(x) for x in a))
'1', '2', '3', '4', '5'

3 Comments

thanks god you answered my question! It was perfect!
but I need the commas as well
Updated. Adding commas back just takes adding a comma to join.
0
", ".join("'{}'".format(x) for x in a)

2 Comments

Welcome to SO. It is always good to provide some context to your answer. Thanks
@LonelyNeuron Thanks for the advice, it will be better next time :)
0

Trying to catch up the train with old school string building solution

a = ['1','2','3','4','5']
out_string=''
for i in a:
    out_string+="'"+ i +"'"+', '
print(out_string.rstrip(', ')) # '1', '2', '3', '4', '5'

Comments

0
a = ['1','2','3','4','5'] 
print([str(item) for item in a])

I like it like this.

1 Comment

That's not what he wants. It's still a list, just items are str, but his intended output is str, not list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.