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
string = "Hello World! How are you?"
words = string.split()
print(words)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 "!"
string = "Hello World! How are you?"
words = string.split("!")
print(words)This will output the following:
second output
['Hello World', ' How are you?']