Home > other >  Why doesn't join return a string?
Why doesn't join return a string?

Time:04-20

def group_list(group, users):
  members = group ":" ", ".join(users)
  return members

print(group_list("Engineering", ["Kim", "Jay", "Tom"]))

I get: Engineering:Kim, Jay, Tom

but was expecting 'Engineering:Kim, Jay, Tom'

CodePudding user response:

You can just think that:

the print function will remove the `'` around the string.

just easy to understand, not strictly correct.

CodePudding user response:

print() removes the quotes ('') from a string because they are not needed. When you print text to the console or screen, the text is always a string, so showing the quotes is redundant and obstructive.


To explore this, open your terminal and run python, then type:

>>> ', '.join(['a','b'])
'a, b'

Which does indeed return a string.

Printing will remove the quotes:

>>> print(', '.join(['a','b']))
a, b
  • Related