본문 바로가기
CS/Python

[Python] String List 내용 문자열(string) 만드는 방법 - join 함수

by Warehaus 2021. 10. 1.

매일 쓰는 내용이라 더 이상 찾고싶지 않아서 쓰는 글입니다.

List 에 string을 보관하게 되는 경우가 많은데, 이럴 떄 밥먹듯이 쓰는 함수가 있습니다.

그것은 바로 join

일단 공신력 있는 레퍼런스를 보고 가시죠. 

그냥 사용법만 보고싶으시면 skip해도 무관합니다.

https://docs.python.org/3/library/stdtypes.html#str.join

 

Built-in Types — Python 3.9.7 documentation

The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some collection classes are mutable. The methods that add, subtract,

docs.python.org

 

아래 예시를 참고하시면  어떻게 하는지 감이 오실거라 생각합니다.

그리고 리스트 항목에 숫자를 섞어서 사용하면 에러가 납니다.

string 자료형만 join이 가능하다는 첨 참고하시기 바랍니다.

$cat 211001.py
# test list
test = [ "I", "am", "not", "fool" ]

# test list 출력
print( test )

# 공백없이 리스트 항목들을 모두 이어줌
print( "".join(test) )

# 공백을 구분자로 리스트 항목을 이어줌
print( " ".join(test) )

# 파이썬 실행결과
$python3 211001.py 
['I', 'am', 'not', 'fool']
Iamnotfool
I am not fool