String को Words की List में Convert करना
String को Words की List में Convert करने के लिए, आपको बस इसे Whitespace पर Split करना होगा। String Class से split() का उपयोग कर सकते हैं। इस Method का Default Delimiter Whitespace होता है, यानी जब String पर Call किया जाता है, तो यह उस String को Whitespace Characters पर Split कर देगा।
उदाहरण
>>> "Please split this string".split() ['Please', 'split', 'this', 'string']
Custom Delimiter के साथ:
>>> "apple,banana,cherry".split(',')
['apple', 'banana', 'cherry']
>>> "a b c".split() # Multiple Spaces Handle करता है
['a', 'b', 'c']
>>> "a b c".split(' ') # केवल Single Space पर Split
['a', '', 'b', '', '', 'c']
Regex का उपयोग (Slower लेकिन Flexible):
>>> import re >>> re.split(r'\s+', 'Please split this string') ['Please', 'split', 'this', 'string']
split() vs splitlines():
>>> "Line1\nLine2\r\nLine3".splitlines()
['Line1', 'Line2', 'Line3']
>>> "Line1\nLine2\r\nLine3".split('\n')
['Line1', 'Line2\r', 'Line3']
Max Splits Limit करना:
>>> "a,b,c,d".split(',', 2)
['a', 'b', 'c,d']