freeCodeCamp/guide/chinese/python/string-methods/string-split-method/index.md

86 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: String Split Method
localeTitle: 字符串拆分方法
---
`split()`函数通常用于Python中的字符串拆分。
#### `split()`方法
模板: `string.split(separator, maxsplit)`
`separator` :分隔符字符串。您可以根据此字符拆分字符串。例如。它可能是 ” ”, ”:”, ”;”等等
`maxsplit` :基于`separator`拆分字符串的次数。如果未指定或-1则根据`separator`所有匹配项拆分字符串
此方法返回的分隔字符串的列表`separator`
#### 例子
1在空格上拆分字符串“”
```python
string = "freeCodeCamp is fun."
print(string.split(" "))
```
输出:
```python
['freeCodeCamp', 'is', 'fun.']
```
2用逗号分隔字符串
```python
string = "freeCodeCamp,is fun, and informative"
print(string.split(","))
```
输出:
```python
['freeCodeCamp', 'is fun', ' and informative']
```
3未指定`separator`符
```python
string = "freeCodeCamp is fun and informative"
print(string.split())
```
输出:
```python
['freeCodeCamp', 'is', 'fun', 'and', 'informative']
```
注意:如果未指定`separator`符,则会删除**所有**空格的字符串
```python
string = "freeCodeCamp is fun and informative"
print(string.split())
```
输出:
```python
['freeCodeCamp', 'is', 'fun', 'and', 'informative']
```
3使用`maxsplit`拆分字符串。这里我们将字符串拆分为两次:
```python
string = "freeCodeCamp is fun and informative"
print(string.split(" ", 2))
```
输出:
```python
['freeCodeCamp', 'is', 'fun and informative']
```
#### 更多信息
查看[有关字符串拆分](https://docs.python.org/2/library/stdtypes.html#str.split)的[Python文档](https://docs.python.org/2/library/stdtypes.html#str.split)