2012-04-27 8 views

답변

6
s = 'hello there' 

word1, word2 = s.split() 

이 작업을 수행합니다. 예를 들어,

In [63]: s = 'hello there' 
In [64]: word1, word2 = s.split() 
In [65]: print word1 
hello 
In [66]: print word2 
there 

split()은 매우 다양합니다, 당신은 또한에 분할 다른 문자를 지정할 수 있습니다. split()에 대한 자세한 내용은 당신은 string.split(s[, sep[, maxsplit]])를 사용해야합니다 http://docs.python.org/library/stdtypes.html?highlight=split#str.split

4

를 참조하십시오

s = "hello world" 
word1, word2 = s.split(' ', 1) 

그것은 인수로 제공하는 문자에 의해리스트로 문자열을 분할합니다. 기본값은 공백이지만 매개 변수로 사용하여 명확하게 만듭니다.

또한 maxsplit 인수를 제공하고 문자열이 maxsplit 번 이상 분할되지 않도록 할 수 있습니다 (예 : 문자열에서와 같이 두 개의 변수로 분할 된 단어를 삽입하기 때문에 하나의 분할이 있어야합니다). .

1
word1, word2 = 'hello there'.split() 
관련 문제