Python Split a String into Multiple Substrings
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)
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'。
Python3 Examples