python學習手冊筆記--第7章--字符串

單引號和雙引號通用
path=r'c:\new\t'    r+''關閉轉義機制
r''不能以單個\結尾    可以:(r'\\'[:-1])
"""......."""多行模塊
substr in str        判斷子串是否在串中
分片
s[-2]        支持負偏移
s[:]        實現頂層拷貝
s[1:10:2]    2是步進        s[::-1]即爲翻轉字符串<==>s[slice(None,None,-1)]
轉換
ascII碼轉換    ord('s')>>>115        chr(115)>>>'s'
list->string:
l=['a','b']        s=''.join(l)>>'ab'
文本解析:
line='aaa bbb ccc'
col=line.split()>>>['aaa', 'bbb', 'ccc']        默認是空格分割
.rstrip()        去掉'\n'
.upper()        變大寫
.statrswith('**')/.endswith('**')        判斷開頭/結尾
字符串格式化表達式:
一版都用%s即可
%[name][flags][width][.precision]typecode        width/precision可以通過指定
'that is %d %s bird' % (1, 'red') >>> 'that is 1 red bird'
基於字典的字符串格式化:
'%(name) hello'%{"name":'cai'}>>>'cai hello'
內置函數vars(),包含調用時存在的變量及對應的值
'this is {0} {2} bird'.format(1,'red')>>>'this is 1 red bird'    <==>     'this is {0} {col} bird'.format(1, col='red')
'my {1[hei]} runs {0.platporm}'.format(sys, {'hei':'laptop'})>>>'my laptop runs linux2'
{**:XX}XX代表格式化的操作
字典健:< > =或^
可變類型:列表,字典,可變集合
不可變類型:數字,字符串,元組,不可變集合



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章