1、Split()and Split(“ ”)and strip()
split() 将是按两个字符串直接的空格分割,split(" ")是按照一个空格进行分割的, strip()去除首尾空格。
>>> a = 'the sky is blue'
>>> b = ' the sky is blue '
>>> a.split()
['the', 'sky', 'is', 'blue']
>>> b.split()
['the', 'sky', 'is', 'blue']
>>> a.split(" ")
['the', 'sky', 'is', 'blue']
>>> b.split(" ")
['', 'the', 'sky', '', 'is', 'blue', '']
>>> b.strip()
'the sky is blue'
2、[::-1] and [:-1]
[::-1] 是将字符串中的每个字母按照逆序输出,[:-1]是截取字符串最后一个字符以前的字符串;
对于列表,逆序输出列表中的每个元素。对于列表来说reverse()函数和[::-1]等价。
>>> a = 'the sky is blue'
>>> a[::-1]
'eulb si yks eht'
>>> a[:-1]
'the sky is blu'
>>> tmp = a.split()
>>> tmp
['the', 'sky', 'is', 'blue']
>>> tmp[::-1]
['blue', 'is', 'sky', 'the']
>>>
3、“ ”.join(list)
将列表中的元素按照空格拼接输出
>>> tmp
['blue', 'is', 'sky', 'the']
>>> " ".join(tmp)
'blue is sky the'