达内致力于面向IT互联网行业,培养软件开发工程师、测试工程师、系统管理员、智能硬件工程师、UI设计师、网络营销工程师、会计等职场人才。2015年起,推出面向青少年的少儿编程、智能机器人编程、编程数学等K12课程
字符串拆分
利用Python中的 split() 方法可以轻易将字符串拆分成较小的子字符串列表。
split() 方法:https://docs.python.org/3/library/stdtypes.html#str.split
s = 'KDnuggets is a fantastic resource'
print(s.split())
['KDnuggets', 'is', 'a', 'fantastic', 'resource']
默认情况下,split()根据空格进行拆分,但同样也可以将其他字符序列传递给split()进行拆分。
s = 'these,words,are,separated,by,comma'print('\',\' separated split -> {}'.format(s.split(',')))
s = 'abacbdebfgbhhgbabddba'print('\'b\' separated split -> {}'.format(s.split('b')))
',' separated split -> ['these', 'words', 'are', 'separated', 'by', 'comma']'b' separated split -> ['a', 'ac', 'de', 'fg', 'hhg', 'a', 'dd', 'a']