python 将字符串以各种分隔符隔开
时间: 2023-12-23 19:06:27 浏览: 80
Sure, I can definitely help you with that! Python provides several built-in string methods that allow you to split a string into a list of substrings based on a specified delimiter. The most commonly used method is the `split()` method, which splits a string into substrings based on whitespace by default, but can also be used to split on any other delimiter. For example:
```
string = "Hello, world! How are you today?"
delimiter1 = ","
delimiter2 = " "
delimiter3 = "o"
# Splitting on comma
print(string.split(delimiter1))
# Splitting on space
print(string.split(delimiter2))
# Splitting on letter 'o'
print(string.split(delimiter3))
```
Output:
```
['Hello', ' world! How are you today?']
['Hello,', 'world!', 'How', 'are', 'you', 'today?']
['Hell', ', w', 'rld! H', 'w are y', 'u t', 'day?']
```
I hope that helps! Do you have any other questions?
阅读全文