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

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

ใช้เมธอด split() default delimiter คือ whitespace

ตัวอย่าง

>> 'aa-ab-ca'.split('-')
['aa', 'ab', 'ca']
>>> 'abc mno rst'.split(' ')
['abc', 'mno', 'rst']

หรือใช้ re.split() (ช้ากว่า)

>> import re
>>> re.split('-', 'aa-ab-ca')
['aa', 'ab', 'ca']