Python splits a string into a list by spaces

Document 对象参考手册Python3 Examples

In Python, we can use thesplit()method to split a string into a list by spaces.split()The method uses spaces as the delimiter by default, splitting the string into multiple parts and returning a list containing these parts.

Example

# Define a string
text = "Hello World Python Programming"

# Use the split() method to split the string into a list by spaces
words = text.split()

# Print the result
print(words)

Code analysis:

  • text = "Hello World Python Programming": Define a stringtext, the content is "Hello World Python Programming".
  • words = text.split(): Use thesplit()method to split the stringtextby spaces into a list, and store the result in variablewords.
  • print(words): Print the resulting listwords。

Output:

Example

['Hello', 'World', 'Python', 'Programming']

Document 对象参考手册Python3 Examples

Other extensions