Convert a list of characters into a string
'a', 'b', 'c'].join('')Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: 'list' object has no attribute 'join'[
Strange enough, in Python the
join method is on the
# this is the Python way"".join(['a','b','c','d'])
join is not a method in the
str.join(list) method means: join the list into a new string using
str as a separator (in this case
str is an empty string).
Somehow I got to love this way of thinking after a while. I can complain about a lot of things in Python design, but not about its coherence.
If your Python interpreter is old (1.5.2, for example, which is common on some older Linux distributions), you may not have
join() available as a method on any old string object, and you will instead need to use the string module. Example:
a = ['a', 'b', 'c', 'd']try: b = ''.join(a)except AttributeError: import string b = string.join(a, '')
b will be