join() function in Python 3 supports only strings? -


python 3, windows 7. found out in python 3 join() function works on strings (what!? why?). need work on anything.

eg.

lista = [1,2,3,"hey","woot",2.44] print (" ".join(lista))  1 2 3 hey woot 2.44 

also tell me why supports strings?

it supporst string becuase output of "".join() in string format:

lista = [1,2,3,"hey","woot",2.44] print (" ".join(map(str,lista))) print type(" ".join(map(str,lista))) 1 2 3 hey woot 2.44 <type 'str'> 

this due fact can not append int/float , string:

i.e.)

"a"+2 --------------------------------------------------------------------------- typeerror                                 traceback (most recent call last) <ipython-input-232-902dbc9d2568> in <module>() ----> 1 "a"+2  typeerror: cannot concatenate 'str' , 'int' objects   lista = [1,2,3,"hey","woot",2.44] print (" ".join(lista))  --------------------------------------------------------------------------- typeerror                                 traceback (most recent call last) <ipython-input-235-f57f2c8c638f> in <module>()     1 lista = [1,2,3,"hey","woot",2.44] ----> 2 print (" ".join(lista))     3   typeerror: sequence item 0: expected string, int found  

when done join:

lista = [1,2,3,"hey","woot",2.44] print (" ".join(lista))  --------------------------------------------------------------------------- typeerror                                 traceback (most recent call last) <ipython-input-235-f57f2c8c638f> in <module>()     1 lista = [1,2,3,"hey","woot",2.44] ----> 2 print (" ".join(lista))     3   typeerror: sequence item 0: expected string, int found  

it stating element @ 0 index int instead of string argument

internally join iterate on list(iterable objects) , append prefix in case " " space , provide string output so "".join() not support int/float


Comments