[toc]
## **字符串基礎知識**
在Python中,加了引號的字符都被認為是字符串,字符串也可以像list列表一樣通過下標引用
```python
>>> name = "Alex Li" #雙引號
>>> age = "22" #只要加引號就是字符串
>>> age2 = 22 #int
>>> msg = '''My name is Alex, I am 22 years old!''' #3個引號也可以
>>> hometown = 'ShanDong' #單引號也可以
```
### 單雙多引號的區別
* 單雙引號的區別
木有任何區別,只有下面這種情況,需要考慮單雙的配合
`msg = "My name is Alex , I'm 22 years old!"
`
* 多引號的作用呢
作用就是多行字符串必須用多引號
```
msg = '''
今天我想寫首小詩,
歌頌我的同桌.
'''
print(msg)
```
### 字符串拼接
字符串呢只能進行"相加"和"相乘"運算。
```python
>>> name
'Alex Li'
>>> age
'22'
>>>
>>> name + age #相加其實就是簡單拼接
'Alex Li22'
>>>
>>> name * 10 #相乘其實就是復制自己多少次,再拼接在一起
'Alex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex Li'
```
> 注意:
> 字符串的拼接只能是雙方都是字符串,不能跟數字或其它類型拼接
> 字符串的單引號和雙引號都無法取消特殊字符的含義,如果想讓引號內所有字符均取消特殊意義,在引號前面加r,如`name=r'l\thf'`
## **字符串常用方法**
### 字符串索引
```
>>> name='hello word'
>>> name[2]
'l'
>>> name[1]
'e'
>>> name.index('o')
4
```
### 字符串切片
```
>>> name='hello word'
>>> name[1:4]
'ell'
>>> name[:4]
'hell'
```
### 判斷是否純數字`isdigit`
```python
>>> name1='222';name2='aaa';name3='22aa'
>>> name1.isdigit(),name2.isdigit(),name3.isdigit()
(True, False, False)
```
### 字符串替換`replace`
```
>>> name='hello word omg'
>>> name.replace('o','X')
'hellX wXrd Xmg'
>>> name.replace('o','X',1) #只替換一個
'hellX word omg'
```
### 字符串查找`find`
```
>>> name='hello word omg'
>>> name.find('o')
4
>>> name.find('o',5) #從第5個開始查找
7
>>> name.rfind('o') #從右邊開始查找
11
```
### 字符串統計`count`
```
>>> name='hello word omg'
>>> name.count('o')
3
>>> name.count('l')
2
```
### 取消前后空格`strip`
```
>>> name=' hello word omg '
>>> name.strip()
'hello word omg'
```
### 字符串居中填補`center`
```
>>> name='hello word omg'
>>> name.center(30,'-') #30個字符長度,不夠就用指定字符代替(默認空格)
'--------hello word omg--------'
>>> name.center(30)
' hello word omg '
```
### 字符串**格式化**`format`
* 用`%s`格式化
```
>>> print('my %s is %s'%('name','noah'))
my name is noah
>>> name='my %s is %s'%('name','noah')
>>> name
'my name is noah'
```
* 用`format`格式化
```
#1.不指定序號
>>> name="my name is {},my age is {}"
>>> name.format('noah',22)
'my name is noah,my age is 22'
#2.指定序號
>>> name="my name is {0},my age is {1},this is {1}"
>>> name.format('noah',22,'haha')
'my name is noah,my age is 22,this is 22'
#3.用變量名代替序號
>>> name="my name is {name},my age is {age}"
>>> name.format(name='noah',age=22)
'my name is noah,my age is 22'
```
### 字符串連接`join`
```
>>> name=['hello','word','ok']
>>> '+'.join(name)
'hello+word+ok'
>>> '-'.join(name)
'hello-word-ok'
```
### 字符串切割`split`
```
>>> name='hello word omg'
>>> name.split() #默認用空格進行切割
['hello', 'word', 'omg']
>>> name.split('o') #指定過切割字符
['hell', ' w', 'rd ', 'mg']
>>> name.split('o',1) #指定切割個數
['hell', ' word omg']
```
- 基礎部分
- 基礎知識
- 變量
- 數據類型
- 數字與布爾詳解
- 列表詳解list
- 字符串詳解str
- 元組詳解tup
- 字典詳解dict
- 集合詳解set
- 運算符
- 流程控制與循環
- 字符編碼
- 編的小程序
- 三級菜單
- 斐波那契數列
- 漢諾塔
- 文件操作
- 函數相關
- 函數基礎知識
- 函數進階知識
- lambda與map-filter-reduce
- 裝飾器知識
- 生成器和迭代器
- 琢磨的小技巧
- 通過operator函數將字符串轉換回運算符
- 目錄規范
- 異常處理
- 常用模塊
- 模塊和包相關概念
- 絕對導入&相對導入
- pip使用第三方源
- time&datetime模塊
- random隨機數模塊
- os 系統交互模塊
- sys系統模塊
- shutil復制&打包模塊
- json&pickle&shelve模塊
- xml序列化模塊
- configparser配置模塊
- hashlib哈希模塊
- subprocess命令模塊
- 日志logging模塊基礎
- 日志logging模塊進階
- 日志重復輸出問題
- re正則表達式模塊
- struct字節處理模塊
- abc抽象類與多態模塊
- requests與urllib網絡訪問模塊
- 參數控制模塊1-optparse-過時
- 參數控制模塊2-argparse
- pymysql數據庫模塊
- requests網絡請求模塊
- 面向對象
- 面向對象相關概念
- 類與對象基礎操作
- 繼承-派生和組合
- 抽象類與接口
- 多態與鴨子類型
- 封裝-隱藏與擴展性
- 綁定方法與非綁定方法
- 反射-字符串映射屬性
- 類相關內置方法
- 元類自定義及單例模式
- 面向對象的軟件開發
- 網絡-并發編程
- 網絡編程SOCKET
- socket簡介和入門
- socket代碼實例
- 粘包及粘包解決辦法
- 基于UDP協議的socket
- 文件傳輸程序實戰
- socketserver并發模塊
- 多進程multiprocessing模塊
- 進程理論知識
- 多進程與守護進程
- 鎖-信號量-事件
- 隊列與生產消費模型
- 進程池Pool
- 多線程threading模塊
- 進程理論和GIL鎖
- 死鎖與遞歸鎖
- 多線程與守護線程
- 定時器-條件-隊列
- 線程池與進程池(新方法)
- 協程與IO模型
- 協程理論知識
- gevent與greenlet模塊
- 5種網絡IO模型
- 非阻塞與多路復用IO實現
- 帶著目標學python
- Pycharm基本使用
- 爬蟲
- 案例-爬mzitu美女
- 案例-爬小說
- beautifulsoup解析模塊
- etree中的xpath解析模塊
- 反爬對抗-普通驗證碼
- 反爬對抗-session登錄
- 反爬對抗-代理池
- 爬蟲技巧-線程池
- 爬蟲對抗-圖片懶加載
- selenium瀏覽器模擬