Split 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:
This will output the following:
['Hello', 'World!', 'How', 'are', 'you?']
Watch a video course
Python - The Practical Guide
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:
This will output the following:
['Hello World', ' How are you?']