1.概述
string 模塊提供了一些用於處理字符串類型的函數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
File
: string
-
example
-
1.py
import
string
text
=
"Monty Python's Flying Circus"
print
"upper"
,
"=>"
, string.upper(text)
#大小寫轉換
print
"lower"
,
"=>"
, string.lower(text)
print
"split"
,
"=>"
, string.split(text)
#字符串分割
print
"join"
,
"=>"
, string.join(string.split(text),
"+"
)
#字符串連接
print
"replace"
,
"=>"
, string.replace(text,
"Python"
,
"Java"
)
#字符串替換
print
"find"
,
"=>"
, string.find(text,
"Python"
), string.find(text,
"Java"
)
#字符串查找
print
"count"
,
"=>"
, string.count(text,
"n"
)
#字符串計數
upper
=
> MONTY PYTHON'S FLYING CIRCUS
lower
=
> monty python's flying circus
split
=
> [
'Monty'
,
"Python's"
, 'Flying
', '
Circus']
join
=
> Monty
+
Python's
+
Flying
+
Circus
replace
=
> Monty Java's Flying Circus
find
=
>
6
-
1
count
=
>
3
import
string
text
=
"Monty Python's Flying Circus"
print
"upper"
,
"=>"
, string.upper(text)
#大小寫轉換
print
"lower"
,
"=>"
, string.lower(text)
print
"split"
,
"=>"
, string.split(text)
#字符串分割
print
"join"
,
"=>"
, string.join(string.split(text),
"+"
)
#字符串連接
print
"replace"
,
"=>"
, string.replace(text,
"Python"
,
"Java"
)
#字符串替換
print
"find"
,
"=>"
, string.find(text,
"Python"
), string.find(text,
"Java"
)
#字符串查找
print
"count"
,
"=>"
, string.count(text,
"n"
)
#字符串計數
upper
=
> MONTY PYTHON'S FLYING CIRCUS
lower
=
> monty python's flying circus
split
=
> [
'Monty'
,
"Python's"
, 'Flying
', '
Circus']
join
=
> Monty
+
Python's
+
Flying
+
Circus
replace
=
> Monty Java's Flying Circus
find
=
>
6
-
1
count
=
>
3
|
在 Python 1.5.2 以及更早版本中, string 使用 strop 中的函數來實現模塊功能.在 Python1.6 和后繼版本,更多的字符串操作都可以作為字符串方法來訪問,string 模塊中的許多函數只是對相對應字符串方法的封裝.
2.使用字符串方法替代 string 模塊函數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
File
: string
-
example
-
2.py
text
=
"Monty Python's Flying Circus"
“upper
", "
=
>", text.upper()
"lower"
,
"=>"
, text.lower()
"split"
,
"=>"
, text.split()
"join"
,
"=>"
,
"+"
.join(text.split())
"replace"
,
"=>"
, text.replace(
"Python"
,
"Perl"
)
"find"
,
"=>"
, text.find(
"Python"
), text.find(
"Perl"
)
"count"
,
"=>"
, text.count(
"n"
)
upper
=
> MONTY PYTHON'S FLYING CIRCUS
lower
=
> monty python's flying circus
split
=
> [
'Monty'
,
"Python's"
, 'Flying
', '
Circus']
join
=
> Monty
+
Python's
+
Flying
+
Circus
replace
=
> Monty Perl's Flying Circus
find
=
>
6
-
1
count
=
>
3
text
=
"Monty Python's Flying Circus"
“upper
", "
=
>", text.upper()
"lower"
,
"=>"
, text.lower()
"split"
,
"=>"
, text.split()
"join"
,
"=>"
,
"+"
.join(text.split())
"replace"
,
"=>"
, text.replace(
"Python"
,
"Perl"
)
"find"
,
"=>"
, text.find(
"Python"
), text.find(
"Perl"
)
"count"
,
"=>"
, text.count(
"n"
)
upper
=
> MONTY PYTHON'S FLYING CIRCUS
lower
=
> monty python's flying circus
split
=
> [
'Monty'
,
"Python's"
, 'Flying
', '
Circus']
join
=
> Monty
+
Python's
+
Flying
+
Circus
replace
=
> Monty Perl's Flying Circus
find
=
>
6
-
1
count
=
>
3
|
為了增強模塊對字符的處理能力, 除了字符串方法, string 模塊還包含了類型轉換函數用於把字符串轉換為其他類型, (如 Example 1-53 所示).
3.使用 string 模塊將字符串轉為數字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
File
: string
-
example
-
3.py
import
string
int
(
"4711"
),
string.atoi(
"4711"
),
string.atoi(
"11147"
,
8
),
# octal 八進制
string.atoi(
"1267"
,
16
),
# hexadecimal 十六進制
string.atoi(
"3mv"
,
36
)
# whatever...
print
string.atoi(
"4711"
,
0
),
print
string.atoi(
"04711"
,
0
),
print
string.atoi(
"0x4711"
,
0
)
print
float
(
"4711"
),
print
string.atof(
"1"
),
print
string.atof(
"1.23e5"
)
4711
4711
4711
4711
4711
4711
2505
18193
4711.0
1.0
123000.0
import
string
int
(
"4711"
),
string.atoi(
"4711"
),
string.atoi(
"11147"
,
8
),
# octal 八進制
string.atoi(
"1267"
,
16
),
# hexadecimal 十六進制
string.atoi(
"3mv"
,
36
)
# whatever...
print
string.atoi(
"4711"
,
0
),
print
string.atoi(
"04711"
,
0
),
print
string.atoi(
"0x4711"
,
0
)
print
float
(
"4711"
),
print
string.atof(
"1"
),
print
string.atof(
"1.23e5"
)
4711
4711
4711
4711
4711
4711
2505
18193
4711.0
1.0
123000.0
|
大多數情況下 (特別是當你使用的是 1.6 及更高版本時) ,你可以使用 int 和float 函數代替 string 模塊中對應的函數。atoi 函數可以接受可選的第二個參數, 指定數基(number base). 如果數基為0, 那么函數將檢查字符串的前幾個字符來決定使用的數基: 如果為 "0x," 數基將為 16 (十六進制), 如果為 "0," 則數基為 8 (八進制). 默認數基值為 10(十進制), 當你未傳遞參數時就使用這個值.在 1.6 及以后版本中, int 函數和 atoi 一樣可以接受第二個參數. 與字符串版本函數不一樣的是 , int 和 float 可以接受 Unicode 字符串對象.
4.一些函數剖析
4.1函數原型strip
聲明:s為字符串,rm為要刪除的字符序列
s.strip(rm) 刪除s字符串中開頭、結尾處,位於 rm刪除序列的字符
s.lstrip(rm) 刪除s字符串中開頭處,位於 rm刪除序列的字符
s.rstrip(rm) 刪除s字符串中結尾處,位於 rm刪除序列的字符
注意:
4.1.1. 當rm為空時,默認刪除空白符(包括'\n', '\r', '\t', ' ')
例如:
>>> a = ' 123'
>>> a.strip()
'123'
>>> a='\t\tabc'
'abc'
>>> a = 'sdff\r\n'
>>> a.strip()
'sdff'
4.1.2.這里的rm刪除序列是只要邊(開頭或結尾)上的字符在刪除序列內,就刪除掉。
例如 :
>>> a = '123abc'
>>> a.strip('21')
'3abc' 結果是一樣的
>>> a.strip('12')
'3abc'
4.2split方法
python 字符串的split方法是用的頻率還是比較多的。比如我們需要存儲一個很長的數據,並且按照有結構的方法存儲,方便以后取數據進行處理。當然可以用json的形式。但是也可以把數據存儲到一個字段里面,然后有某種標示符來分割比如我們的存儲的格式的:
格式的:
姓名,年齡|另外一個用戶姓名,年齡
name:haha,age:20|name:python,age:30|name:fef,age:55
那我們可以通過字符串對象的split方法切割字符串對象為列表。
a = 'name:haha,age:20|name:python,age:30|name:fef,age:55'
print a.split('|')
返回結果:
['name:haha,age:20', 'name:python,age:30', 'name:fef,age:55']
#列表對象自動用逗號分割開來
4.3join方法
list = [1, 2, 3, 4, 5, 6, 7]
','.join(str(i) for i in list) #str(i) for i in list 為啥這么寫可以執行成功,
join是string類型的一個函數,用調用他的字符串去連接參數里的列表‘,'.join調用者是',',python里面萬物皆對象,','是一個string類型的對象,調用join函數,將后面的列表里的值用逗號連接成新的字符串;str(i) for i in list 這是一個映射,就是把list中每個值都轉換成字符串。如果你要str(i) for i in list的結果是['1', '2', '3', '4', '5', '6', '7']
string.join(sep): 以string作為分割符,將sep中所有的元素(字符串表示)合並成一個新的字符串
>>>li = ['my','name','is','bob']
>>>' '.join(li)
'my name is bob'
>>>'_'.join(li)
'my_name_is_bob'
>>> s = ['my','name','is','bob']
>>> ' '.join(s)
'my name is bob'
>>> '..'.join(s)
'my..name..is..bob'