Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Split String ด้วย Whitespace ใน Python

ใช้เมธอด split() ของ string class ค่า default delimiter คือ whitespace

ตัวอย่าง

>> "Please split this string".split()
['Please', 'split', 'this', 'string']

หรือใช้ regex re.split(r'\s+', string) แต่ช้ากว่า

>> import re
>>> re.split(r'\s+', 'Please split this string')
['Please', 'split', 'this', 'string']