Wednesday 19 February 2020

Join a list of items with different types as string in Python

Calling str(...) is the Pythonic way to convert something to a string.
You might want to consider why you want a list of strings. You could instead keep it as a list of integers and only convert the integers to strings when you need to display them. For example, if you have a list of integers then you can do this:
print ', '.join(str(x) for x in list_of_ints)

from : https://stackoverflow.com/questions/3590165/join-a-list-of-items-with-different-types-as-string-in-python

No comments:

Post a Comment