Python Split a String into a List by Spaces
In Python, we can use split() The method splits a string into a list by spaces.split() By default the method uses a space as the separator, splits the string into several parts, and returns a list containing those parts.
Example
Define a string
text
=
"Hello World Python Programming"
Use the split() method to split a string into a list by spaces
words
=
text.
split
(
)
Print the result
(
words
)
Code analysis:
text = "Hello World Python Programming": define a stringtext, with the content "Hello World Python Programming".words = text.split(): When usingsplit()The method splits the stringtextinto a list by spaces and stores the result in the variablewordsit.print(words): Print the split listwords。
Output result:
Example
[
'Hello'
,
'World'
,
'Python'
,
'Programming'
]
Python3 Examples