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:
- Python3 urlopen() TypeError: can’t convert ‘bytes’ object to str im…
- AttributeError: ‘list’ object has no attribute ‘keys’
- How to Solve Libpng warning ICCP error
- Convert Object to List>, avoiding Unchecked cast: ‘java.lang.Object’ to ‘java.util.List
- [leetcode] 140. Word break II word split II
- Pytest @pytest.mark.parametrize Example
- [Solved] TypeError: Property value expected type of string but got null
- Pytorch: How to Use pack_padded_sequence & pad_packed_sequence
- [Solved] Python TypeError: expected a string or other character buffer object
- [Solved] Exception in thread “main” java.util.ConcurrentModificationException