How can I use .format with a list?

65 views Asked by At

I'm still a beginner at Python, and was doing some simple code just to exercise. I defined a list with some names, so I could print it afterwards, but I can't really do it the way I want.

I did it kind of like this:

names = ['John', 'Karl', 'Anne', 'Beth']
print("Those kids' names are {}.".format(names))

It returns "Those kids' names are ['John', 'Karl', 'Anne', 'Beth']."

I wanted it to be displayed without the brackets ([]) and the quotes (''), like this: "Those kids' names are John, Karl, Anne, Beth."

Is there a way to do that?

1

There are 1 answers

0
AKX On

Use str.join to join an iterable of strings with a given separator.

names = ['John', 'Karl', 'Anne', 'Beth']
print(f"Those kids' names are {', '.join(names)}.")