Python字符串分隔

split() 方法可以对字符串进行分隔切片

str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。 num -- 分割次数。默认为 -1, 即分隔所有。

str = "this is string example..."

print(str.split( ))	# 以空格分隔
	['this', 'is', 'string', 'example...']

print(str.split(" ", 1))	# 以空格分隔,只分隔一次
	['this', 'is string example...']

print(str.split("s"))	# 以字母分隔
	['thi', ' i', ' ', 'tring example...']
print(str.split( )[0])
	this
	

最后更新于