매일 쓰는 내용이라 더 이상 찾고싶지 않아서 쓰는 글입니다.
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
'CS > Python' 카테고리의 다른 글
[python] 오류출력은 에러로 보내주세요 ! - print 함수의 출력을 stderr 로 보내기 (2) | 2021.10.06 |
---|---|
[python] python에서 객체를 string으로 출력하는 방법 (0) | 2021.10.04 |
[Python unittest] Python 으로 Unit test 만드는 방법 2 - assert 종류 (0) | 2021.07.26 |
[Python unittest] Python 으로 Unit test 만드는 방법 1 (0) | 2021.07.20 |
Python 개행문자 지우는 방법, strip 과 replace (0) | 2021.07.08 |