본문 바로가기
python-기타

split(), split(" ")의 차이점

by 무적김두칠 2022. 10. 26.

결론부터 말씀드리면

split()은 빈 문자열은 죽이고 , split(" ")은 살립니다.
In conclusion, split() discards empty string, split(" ") do not

 

1
2
3
s=' abc bbb c d  e '
print(s.split())
print(s.split(' '))
cs

 

위와 같은 코드를 실행시키면 
I ran the above code

이렇게 나옵니다('d' 와 'e' 사이는 공백이 2칸입니다!)
Here is the result (Between 'd' and 'e' is Two spaces )

따라서 문자열만 필요하다 할때는 split()을 쓰셔도 되겠지만
space 도 필요하다 판단되시면 split(' ')을 쓰셔야 겠습니다.

So If you need only strings , You can use split()
else if you need space too, You should use split(' ')

 

1
print (''.split.__doc__)
cs

 

split 에 대한 내용을 읽어보면 아래와 같습니다
This is document of split()

반응형

댓글