# å符串
åç¬¦ä¸²ä¹æ¯ä¸ç§åºåï¼å æ¤ï¼éç¨çåºåæä½ï¼æ¯å¦ç´¢å¼ï¼åçï¼å æ³ï¼ä¹æ³ç对å®åæ ·éç¨ãæ¯å¦ï¼
```python
>>> s = 'hello, '
>>> s[0] # ç´¢å¼
'h'
>>> s[1:3] # åç
'el'
>>> s + 'world' # å æ³
'hello, world'
>>> s * 2 # 乿³
'hello, hello, '
```
ä½éè¦æ³¨æçæ¯ï¼å符串åå
ç»ä¸æ ·ï¼ä¹æ¯ä¸å¯åçï¼æä»¥ä½ ä¸è½å¯¹å®è¿è¡èµå¼çæä½ï¼
```python
>>> s = 'hello'
>>> s[1] = 'ab' # ä¸è½å¯¹å®è¿è¡èµå¼
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
```
é¤äºéç¨çåºåæä½ï¼åç¬¦ä¸²è¿æèªå·±çæ¹æ³ï¼æ¯å¦ join, lower, upper çãåç¬¦ä¸²çæ¹æ³ç¹å«å¤ï¼è¿éåªä»ç»ä¸äºå¸¸ç¨çæ¹æ³ï¼å¦ä¸ï¼
- find
- split
- join
- strip
- replace
- translate
- lower/upper
# find
find æ¹æ³ç¨äºå¨ä¸ä¸ªåç¬¦ä¸²ä¸æ¥æ¾å串ï¼å®è¿åå串æå¨ä½ç½®çæå·¦ç«¯ç´¢å¼ï¼å¦ææ²¡ææ¾å°ï¼åè¿å -1ã
ççä¾åï¼
```python
>>> motto = "to be or not to be, that is a question"
>>> motto.find('be') # è¿å 'b' æå¨çä½ç½®ï¼å³ 3
3
>>> motto.find('be', 4) # æå®ä»èµ·å§ä½ç½®å¼å§æ¾ï¼æ¾å°çæ¯ç¬¬ 2 个 'be'
16
>>> motto.find('be', 4, 7) # æå®èµ·å§ä½ç½®åç»ç¹ä½ç½®ï¼æ²¡ææ¾å°ï¼è¿å -1
-1
```
# split
split æ¹æ³ç¨äºå°å符串å岿åºåã
ççä¾åï¼
```python
>>> '/user/bin/ssh'.split('/') # ä½¿ç¨ '/' ä½ä¸ºåé符
['', 'user', 'bin', 'ssh']
>>> '1+2+3+4+5'.split('+') # ä½¿ç¨ '+' ä½ä¸ºåé符
['1', '2', '3', '4', '5']
>>> 'that is a question'.split() # 没ææä¾åå²ç¬¦ï¼é»è®¤ä½¿ç¨ææç©ºæ ¼ä½ä¸ºåé符
['that', 'is', 'a', 'question']
```
éè¦æ³¨æçæ¯ï¼å¦æä¸æä¾åé符ï¼åé»è®¤ä¼ä½¿ç¨ææç©ºæ ¼ä½ä¸ºåé符ï¼ç©ºæ ¼ãå¶è¡¨ç¬¦ãæ¢è¡çï¼ã
# join
join æ¹æ³å¯ä»¥è¯´æ¯ split çéæ¹æ³ï¼å®ç¨äºå°åºåä¸çå
ç´ è¿æ¥èµ·æ¥ã
ççä¾åï¼
```python
>>> '/'.join(['', 'user', 'bin', 'ssh'])
'/user/bin/ssh'
>>>
>>> '+'.join(['1', '2', '3', '4', '5'])
'1+2+3+4+5'
>>> ' '.join(['that', 'is', 'a', 'question'])
'that is a question'
>>> ''.join(['h', 'e', 'll', 'o'])
'hello'
>>> '+'.join([1, 2, 3, 4, 5]) # ä¸è½æ¯æ°å
Traceback (most recent call last):
File "", line 1, in
TypeError: sequence item 0: expected string, int found
```
# strip
strip æ¹æ³ç¨äºç§»é¤å符串左å³ä¸¤ä¾§çç©ºæ ¼ï¼ä½ä¸å
æ¬å
é¨ï¼å½ç¶ä¹å¯ä»¥æå®éè¦ç§»é¤çå符串ã
ççä¾åï¼
```python
>>> ' hello world! '.strip() # ç§»é¤å·¦å³ä¸¤ä¾§ç©ºæ ¼
'hello world!'
>>> '%%% hello world!!! ####'.strip('%#') # ç§»é¤å·¦å³ä¸¤ä¾§ç '%' æ '#'
' hello world!!! '
>>> '%%% hello world!!! ####'.strip('%# ') # ç§»é¤å·¦å³ä¸¤ä¾§ç '%' æ '#' æç©ºæ ¼
'hello world!!!'
```
# replace
replace æ¹æ³ç¨äºæ¿æ¢å符串ä¸ç**ææ**å¹é
项ã
ççä¾åï¼
```python
>>> motto = 'To be or not To be, that is a question'
>>> motto.replace('To', 'to') # ç¨ 'to' æ¿æ¢ææç 'To'ï¼è¿åäºä¸ä¸ªæ°çå符串
'to be or not to be, that is a question'
>>> motto # ååç¬¦ä¸²ä¿æä¸å
'To be or not To be, that is a question'
```
# translate
translate æ¹æ³å replace æ¹æ³ç±»ä¼¼ï¼ä¹å¯ä»¥ç¨äºæ¿æ¢å符串ä¸çæäºé¨åï¼ä½ **translate æ¹æ³åªå¤çå个å符**ã
translate æ¹æ³ç使ç¨å½¢å¼å¦ä¸ï¼
```python
str.translate(table[, deletechars]);
```
å
¶ä¸ï¼table æ¯ä¸ä¸ªå
å« 256 个å符ç转æ¢è¡¨ï¼å¯éè¿ maketrans æ¹æ³è½¬æ¢èæ¥ï¼deletechars æ¯å符串ä¸è¦è¿æ»¤çå符éã
ççä¾åï¼
```python
>>> from string import maketrans
>>> table = maketrans('aeiou', '12345')
>>> motto = 'to be or not to be, that is a question'
>>> motto.translate(table)
't4 b2 4r n4t t4 b2, th1t 3s 1 q52st34n'
>>> motto
'to be or not to be, that is a question'
>>> motto.translate(table, 'rqu') # ç§»é¤ææç 'r', 'q', 'u'
't4 b2 4 n4t t4 b2, th1t 3s 1 2st34n'
```
å¯ä»¥çå°ï¼maketrans æ¥æ¶ä¸¤ä¸ªåæ°ï¼ä¸¤ä¸ªçé¿çå符串ï¼è¡¨ç¤ºç¬¬ä¸ä¸ªåç¬¦ä¸²çæ¯ä¸ªå符ç¨ç¬¬äºä¸ªå符串对åºä½ç½®çå符æ¿ä»£ï¼å¨ä¸é¢çä¾åä¸ï¼å°±æ¯ 'a' ç¨ '1' æ¿ä»£ï¼'e' ç¨ '2' æ¿ä»£ï¼ççï¼æ³¨æï¼æ¯å个å符ç代æ¿ï¼è䏿¯æ´ä¸ªåç¬¦ä¸²çæ¿ä»£ãå æ¤ï¼motto ä¸ç o é½è¢«æ¿æ¢ä¸º 4ï¼e é½è¢«æ¿æ¢ä¸º 2ï¼ççã
# lower/upper
lower/upper ç¨äºè¿åå符串ç大åæå°åå½¢å¼ã
ççä¾åï¼
```python
>>> x = 'PYTHON'
>>> x.lower()
'python'
>>> x
'PYTHON'
>>>
>>> y = 'python'
>>> y.upper()
'PYTHON'
>>> y
'python'
```
# å°ç»
- å符串æ¯ä¸å¯å对象ï¼è°ç¨å¯¹è±¡èªèº«çä»»ææ¹æ³ï¼ä¹ä¸ä¼æ¹å该对象èªèº«çå
容ãç¸åï¼è¿äºæ¹æ³ä¼å建æ°ç对象并è¿åã
- translate é对å个å符è¿è¡æ¿æ¢ã
# åèèµæ
- ãpython åºç¡æç¨ã