split a string into an array of words in python

To split a string into an array of words in Python, you can use the split() method.

Here's an example:

main.py
sentence = "hello world, how are you?"
words = sentence.split()  # Split the sentence into an array of words

print(words)  # Output: ['hello', 'world,', 'how', 'are', 'you?']
176 chars
5 lines

By default, the split() method splits the string by whitespace. You can also specify a delimiter character as an argument to split(), like this:

main.py
sentence = "hello|world|how|are|you?"
words = sentence.split('|')  # Split the sentence by the "|" character

print(words)  # Output: ['hello', 'world', 'how', 'are', 'you?']
175 chars
5 lines

gistlibby LogSnag