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:

string = "Hello World! How are you?"
words = string.split()
print(words)

This will output the following:

['Hello', 'World!', 'How', 'are', 'you?']

Watch a 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:

string = "Hello World! How are you?"
words = string.split("!")
print(words)

This will output the following:

['Hello World', ' How are you?']