Python Split a String into Multiple Substrings

Document 对象参考手册Python3 Examples

In Python, you can use thesplit()method to split a string into multiple substrings.split()The method splits the string according to the specified delimiter and returns a list containing all substrings. If no delimiter is specified, space is used as the delimiter by default.

Example

# Example string
text = "Hello World, this is Python"

# Use the split() method to split the string
result = text.split()

# Output the result
print(result)

Code explanation:

  • text = "Hello World, this is Python": Define a stringtext。
  • result = text.split(): Usesplit()method to split the stringtextby spaces into multiple substrings, and store the result inresultthe list.
  • print(result): Output the result after splitting.

Output result:

['Hello', 'World,', 'this', 'is', 'Python']

In this example, the stringtextwas split into 5 substrings, which are'Hello', 'World,', 'this', 'is', and'Python'。

Document 对象参考手册Python3 Examples

Other extensions