capypad
0 day streak
python / beginner
Snippet

Splitting Strings into Lists

The .split() method breaks a string into a list of smaller strings based on a specific character called a separator.

snippet.py
python
1
2
3
sentence = "Python is fun"
words = sentence.split(" ")
print(words)
Breakdown
1
sentence = "Python is fun"
Defines a string variable with spaces between words.
2
words = sentence.split(" ")
Splits the string everywhere a space character is found.
3
print(words)
Outputs the list ['Python', 'is', 'fun'].