Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Split a String into a List by Spaces

Python Split a String into a List by Spaces

Document Object Reference Manual Python3 Examples

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

print

(

words

)

Code analysis:

Output result:

Example

[

'Hello'

,

'World'

,

'Python'

,

'Programming'

]

Document Object Reference Manual Python3 Examples