[Solved] Python TypeError: sequence item 0: expected str instance, int found

TypeError: sequence item 0: expected str instance, int found

code

list1=[1,'two','three',4]
print(' '.join(list1))

I thought I would print 1 two three 4

It turned out to be a mistake

Traceback (most recent call last):
 File "<pyshell#27>", line 1, in <module>
  print(" ".join(list1))
TypeError: sequence item 0: expected str instance, int found

I checked the information on the Internet and said that the list contains numbers and cannot be directly converted into a string

Solution:

print(" ".join('%s' %id for id in list1))

That is to traverse the elements of the list and convert it into a string. In this way, we can successfully output the result of 1 two three 4

from: https://blog.csdn.net/laochu250/article/details/67649210

Similar Posts: