- 轉(zhuǎn)義符 \ 如果要想在字符串中包含這個(gè)字符志笼,需要這樣寫 ‘\’ print(‘he said, it\’s fine')
- 轉(zhuǎn)義符號(hào)\的另外兩個(gè)形式是和t、n連起來用氓侧,\t代表制表符(就是tab) \n表示換行(就是ennter)
- 字符串的操作符:拼接+瓢喉,復(fù)制*摹蘑,邏輯 in /not in
- 字符串屬于有序容器,下標(biāo)索引
- 處理字符串的內(nèi)建函數(shù):ord()扰法,input()蛹含,float(),len()塞颁,print()
拼接 分隔 替換
創(chuàng)建一個(gè)string 類型的array浦箱,a=[],然后a.append('b')
str.replace(old, new[, max]) 字符串替換
str.expandtabs(tabsize=8) 把字符串中的tab符號(hào)轉(zhuǎn)為空格祠锣,tab符號(hào)(‘\t’) 默認(rèn)空個(gè)為8
指定字符串進(jìn)行分隔 str.split(',') ---> 指定用逗號(hào)分隔
序列中的元素以指定字符連接生成一個(gè)新的字符串
str = ‘-’
seq = ("a","b","c")
str.join(seq) --> 'a-b-c'
list 中只取unique: a=list(set(list))
檢測字符串中是否子字符串酷窥,以特定前綴、后綴結(jié)尾
法1: substr in string
法2: str.find(str) --> 如果包含子字符串返回開始的索引值伴网,否則返回-1
str.startswith('aaa')
str.endswith('xxx')
去空格
str.strip() 讀取時(shí)候去掉后面的\n 等轉(zhuǎn)義字符
str.lstrip() 去左側(cè)空格
str.rstrip() 去右側(cè)空格
大小寫轉(zhuǎn)換
str.upper() # 把所有字符中的小寫字母轉(zhuǎn)換成大寫字母
str.lower() # 把所有字符中的大寫字母轉(zhuǎn)換成小寫字母
str.capitalize() # 把第一個(gè)字母轉(zhuǎn)化為大寫字母蓬推,其余小寫
str.title() # 把每個(gè)單詞的第一個(gè)字母轉(zhuǎn)化為大寫,其余小寫
統(tǒng)計(jì)字符串中某個(gè)字符出現(xiàn)的次數(shù)
str.count('i')
格式化輸出:
使用 % 輸出
浮點(diǎn)數(shù)輸出:
%.3f 保留3位小數(shù)位
%.3e 保留3位小數(shù)位澡腾,使用科學(xué)技術(shù)法
字符串輸出:
%10s 右對齊沸伏,占位符10位 %-10s 左對齊,占位符10位 %.2s 取2位
不帶編號(hào),即{} 蛋铆,
帶數(shù)字編號(hào)馋评,可調(diào)換順序放接,即“{1} {2}”
print('{1} {1} {0}'.format('hello','world'))
world world hello
print("{0:0.1f} hours".format(time))
np.set_printoptions(precision=2) (保留小數(shù)點(diǎn)后幾位)
np.set_printoptions(formatter={'float': lambda x: format(x, '6.2e')})
plt.text(0.01, 0.75, 'dct={}'.format(round(dct,5)))
print('wave length = {:.4f} km'.format(wave_length / 1e5))