I want to do something similar to R’s paste function. For example,
> paste("a", "b", "c", sep = ",")
[1] "a,b,c"
In Python, I can do
''.join(i + ',' for i in ['a', 'b', 'c'])
#'a,b,c,'
However, I don’t want the last comma. Is there a more convenient way to convert the list to a string?
>Solution :
You can pass in any iterable into str.join. There is no need for the generator expression.
https://docs.python.org/3/library/stdtypes.html#str.join
Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.
','.join(['a', 'b', 'c']) returns 'a,b,c'.