Split string on whitespace in Python
You can use the split() method to split a string on whitespace in Python.
You can use the split() method to split a string on whitespace in Python. Here is an example code snippet:
Split a string on whitespace in Python
python— editable, runs on the server
This will output the following:
output
['Hello', 'World!', 'How', 'are', 'you?']The split() method by default splits a string on whitespace, so you don't need to provide any arguments. If you want to split the string on a different delimiter, you can provide that delimiter as an argument to the split() method. For example, to split the string on the character "!", you would use:
Split the string on the character "!"
python— editable, runs on the server
This will output the following:
second output
['Hello World', ' How are you?']