+ 鏈接兩個字符串
+ 一個字符串太長時用做空白字符鏈接
十分長的字符串,用”“” “”“括起來
常用方法:
capitalize() 首字母大寫,其餘小寫
lower() 全部小寫
upper() 全部大寫
swapcase() 大小寫互換
string 採用list 獲得子串
判斷字符串類別:
s.isalnum() 都是字母或者數字
s.isalpha() 都是字母
s.isdigit() 都是數字
s.islower() 都是小寫字母
s.isupper() 都是大寫字母
s.istitle() 所有單詞都是首字母大寫
s.isspace() 所有字符都是空白字符:\n,\t,\r,’ ‘
查找子串:
s.find(substring,[start [,end]]) 返回最先找到的索引值,沒有找到返回-1
s.rfind(substring,[start [,end]]) 反向查找 類似find
s.index(substring,[start [,end]] 類似find 但找不到產生ValueError的異常
s.rindex 反向查找
格式化字符串
% (tuple)
print “%s’s height is %dcm”%(“Charles”,180)
%[(mapping key)][(conversion flag)][(Minimum field width)] |
[(precision)][(lenght modifier)](conversion type)
conversion type 是必須的
“%o and %o” % (16,8) —》20 and 10 八進制
“%u and %u” %(-10,10)—》’4294967286 and 10’ 無符號整數
“%x and %x” % (100,200) —-》 ‘64 and c8’ 16進制小寫
“%X and %X” % (100,200) —》 ‘64 and C8’ 16進制大寫
“%e and %e”%(-100,200) —》2.000000e+02 e浮點數小寫
“%E and %E”%(-100,200)—-》2.000000E+02 E浮點數大寫
“%f and %f”%(-0.00001,200.0)—》’-0.000010 and 200.000000’ 浮點數
“%g and %g”%(-0.00001,2000000.0) —》 ‘-1e-05 and 2e+06’ 浮點數
“%c and %c”%(67,68) —》 ‘C and D’ 把ASCII碼整數變爲字符
“%r”%({“one”:1,”two”:2}) —> “{‘two’: 2, ‘one’: 1}”
字符串 ========print expr(obj)
“%s”%({“one”:1,”two”:2}) —》”{‘two’: 2, ‘one’: 1}”
字符串 ======== print str(obj)
使用maping key的形式
print “%(name)s’s height is %(height)d cm” \
“,%(name)s’s weight is %(weight)d kg” % \
{“name”:”Charles”,”height”:170,”weight”:70}
Charles’s height is 170 cm,Charles’s weight is 70 kg
—-減少重複
用到local函數當中
def fun(a,b):
print "a is %(a)d,b is %(b)d"%locals()
>>>fun(1,2)
a is 1,b is 2
conversion flag的作用:
>>>print “%#x,%#X,%#o”%(16,16,16)
0x10,0X10,020 #號表示用alternate form
join 合併
split分解