How to Split a String by Space in Python?

Estimated read time 1 min read

You can split a string by space in Python by using the split() method. Here’s an example:

string = "The quick brown fox jumps over the lazy dog"
words = string.split()

print(words)  # ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

In this example, we create a string that contains a sentence with several words separated by spaces. We then call the split() method on the string, which splits the string by whitespace (including spaces, tabs, and newlines) and returns a list of words. We assign this list to the variable words, and print it to the console.

By default, the split() method splits the string by any whitespace character. However, you can also specify a different delimiter as an argument to the method. For example, string.split(',') would split the string by commas instead of spaces.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply