user14811040
user14811040

Reputation:

How do I make it so when I print a Python list it doesn't have the single quotes and brackets with each item when I print the list?

Ok so I have been coding Python and when I run this code

names = ["Harry", " Ron", "Hermione"]
print(names)
# output: ['Harry', ' Ron', 'Hermione']

How do I make it so it doesn't have the [] and the ' '. Thanks!

Upvotes: 0

Views: 56

Answers (5)

frankr6591
frankr6591

Reputation: 1247

print(', '.join([n.strip() for n in names]))

or (tks to @Ryan Haining)

print(*[n.strip() for n in names]) 

Upvotes: 0

RufusVS
RufusVS

Reputation: 4137

Two ways come to mind if you want to maintain the comma separator:

print(*names,sep=",")

or

print(','.join(names))

Upvotes: 1

Laggs
Laggs

Reputation: 386

print(", ".join(names))

Upvotes: 1

Rajat Mishra
Rajat Mishra

Reputation: 3780

Depending upon you want the output to be, you can use

print(*names,sep=' ')

by default sep value is ' '. If you want to print all elements in new line then pass '\n' as sep.

Upvotes: 1

sal
sal

Reputation: 74

This should do it

names = ["Harry", " Ron", "Hermione"]

for name in names:
    print(name, end=" ")

Upvotes: 0

Related Questions