Joining elements into a string
Posted: Sun Jun 07, 2009 10:59 am
So Python let's you say:
But I always found that rather strange as I think first about the items. Also it disallows having two separators such as ", " but with "and" for the last separator. So I'm planning:
I think one of the problems in Python is that there is no common base type for "list-like objects" but in .NET and JVM we can put an extension method on IEnumerable/Iterable.
- Code: Select all
print ", ".join(items)
But I always found that rather strange as I think first about the items. Also it disallows having two separators such as ", " but with "and" for the last separator. So I'm planning:
print items.joined(", ")
print words.joined(", ", " and ")
I think one of the problems in Python is that there is no common base type for "list-like objects" but in .NET and JVM we can put an extension method on IEnumerable/Iterable.