취미와 밥줄사이

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

Python

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

취미와 밥줄사이 2021. 11. 29. 00:46

파이썬에서 value가 str이 아닌 list를 문자열로 변환

join 메소드에는 주어진 매개 변수로 str 데이터 유형이 필요합니다. 따라서 int 형식 목록에 가입하려고 하면 TypeError가 표시됩니다.

 

number = [4, 2, 3]
"".join(number)

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

int 타입은 결합되기 전에 str 타입으로 변환되어야 합니다.

 

방법1

number = [4, 5, 6]
"".join([str(_) for _ in number])
"456"

방법2

number = [4, 5, 6]
"".join(map(str, number))
"456"

 

REFERENCE

https://www.delftstack.com/ko/howto/python/how-to-convert-a-list-to-string/

 

파이썬에서리스트를 문자열로 변환하는 방법

이 튜토리얼은 파이썬에서리스트를 문자열로 변환하는 방법을 보여줍니다

www.delftstack.com

 

'Python' 카테고리의 다른 글

[Python] 소수점 자리 제한 방법  (0) 2021.12.02
[Python] 목, 나머지 구하기  (0) 2021.11.29
[Python] Split  (0) 2021.11.28
[Python] map 함수 사용법  (0) 2021.11.28
[Python] 파이썬 디버거 사용법  (0) 2021.11.22