Skip to content

Commit

Permalink
modify
Browse files Browse the repository at this point in the history
  • Loading branch information
WuShichao committed Oct 5, 2018
1 parent d64c0bf commit b3ff450
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions 04.basis.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,53 @@ r"Newlines are indicated by \n"
>
> 在处理正则表达式时应全程使用原始字符串。否则,将会有大量 Backwhacking 需要处理。举例说明的话,反向引用可以通过 `'\\1'``r'\1'` 来实现。
### 字符串方法

我们可以通过使用字符串方法来对字符串进行操作。
* _.strip(): 去除首尾空白字符_
* _.split(): 分割字符串(默认为空格)_
* _.replace(): 替换字符_
* _.find(): 查找字符_
* _.count(): 字符计数_
* _.upper()/.lower(): 转大/小写_
* _.ljust()/rjust()/zfill(): 指定宽度_
* _.isalpha()/isdigit()/.isalnum()_

现在我们通过几个例子来学习一下:

#### join & split
```python
>>>"+".join(['a','b','c'])
a+b+c
>>>"a+b+c".split("+")
['a','b','c']
>>>"I'm fine".split()
["I'm","fine"]
>>>"I'm fine".split("'")
["I","m fine"]
```

#### replace & find
```python
a="abcdacd"
a.replace('a' ,'b')
a.replace('a' ,'b').replace('b' ,'c')
a.find ('a')
a.rfind('a')
a.count('a')
```

#### isalnum
```python
a='hello'
help(a.isalnum)
print(a.isalnum())
print("1234".isalnum())
print("123abc".isalnum())
print("1.23".isalnum())
print("123.abc".isalnum())
```

## 数据类型

变量可以将各种形式的值保存为不同的_数据类型(Data Type)_。基本的类型是我们已经讨论过的数字与字符串。在后面的章节中,我们会了解如何通过 [类(Classes)](https://github.com/WuShichao/a-byte-of-python-bnu/tree/4e7952bd0b5a028cd3149f9b9cff837f08531314/14.oop.md#classes) 类创建我们自己的类型。
Expand Down

0 comments on commit b3ff450

Please sign in to comment.