.strip(' ')
pythonが非改行空白を削除しないのはなぜ.split(' ')
ですか? 文字で分割しますか?
質問する
1571 次
1 に答える
4
まず、これらの関数は 2 つの異なるタスクを実行します。
' foo bar '.split() is ['foo','bar']
#a list of substrings without any white space (Note the default is the look for whitespace - see below)
' foo bar '.strip() is 'foo bar'
#the string without the beginning and end whitespace
.
先頭と末尾のスペースstrip(' ')
のみを使用すると削除されますが、非常に似ていますが、まったく同じではありません。たとえば、空白であるがスペースではないタブの場合です。strip()
\t
' \t foo bar '. strip() is 'foo bar'
' \t foo bar '. strip(' ') is '\t foo bar'
これを使用すると、文字列を各スペースsplit(' ')
に対してリストに「分割」し、各「空白」に対して文字列をリストに分割します。考慮してください(fooとbarの間に2つのスペースがあります)。split()
'foo bar'
'foo bar'.split() is ['foo', 'bar']
#Two spaces count as only one "whitespace"
'foo bar'.split(' ') is ['foo', '', 'bar']
#Two spaces split the list into THREE (seperating an empty string)
2 つのスペース、または複数のスペースとタブが「1 つの空白」としてカウントされるという微妙な点があります。
于 2012-09-06T08:36:46.800 に答える