數據類型
字符串git
21.將"hello world"轉換爲首字母大寫"Hello World"app
a=hello world print(a.title())
22.如何檢測字符串中只含有數字?函數
a="112222ffffff" if a.isdigit() is True: print("只含有數字") else: print("不僅含有數字")
23.將字符串"ilovechina"進行反轉ui
a="ilovechina" print(a[::-1])
24.Python 中的字符串格式化方式你知道哪些?this
%s 字符串 (採用str()的顯示) %r 字符串 (採用repr()的顯示) %c 單個字符 %b 二進制整數 %d 十進制整數 %i 十進制整數 %o 八進制整數 %x 十六進制整數 %e 指數 (基底寫爲e) %E 指數 (基底寫爲E) %f 浮點數 %F 浮點數,與上相同 %g 指數(e)或浮點數 (根據顯示長度) %G 指數(E)或浮點數 (根據顯示長度) %% 字符"%"
25.有一個字符串開頭和末尾都有空格,好比「 adabdw 」,要求寫一個函數把這個字符串的先後空格都去掉。編碼
26.獲取字符串」123456「最後的兩個字符。spa
a="123456" print(a[4:6])
27.一個編碼爲 GBK 的字符串 S,要將其轉成 UTF-8 編碼的字符串,應如何操做?code
decode('gbk').encode('utf-8')
28.s="info:xiaoZhang 33 shandong",用正則切分字符串輸出['info', 'xiaoZhang', '33', 'shandong']blog
import re a="info:xiaoZhang 33 shandong" b=re.findall(r'[^:\s]+', a) print(b)
27.怎樣將字符串轉換爲小寫?ip
a="SSssSSss" print(a.lower())
28.單引號、雙引號、三引號的區別?
先說1雙引號與3個雙引號的區別,雙引號所表示的字符串一般要寫成一行 如: s1 = "hello,world" 若是要寫成多行,那麼就要使用/ (「連行符」)吧,如 s2 = "hello,/ world" s2與s1是同樣的。若是你用3個雙引號的話,就能夠直接寫了,以下: s3 = """hello, world, hahaha.""",那麼s3實際上就是"hello,/nworld,/nhahaha.", 注意「/n」,因此, 若是你的字符串裏/n不少,你又不想在字符串中用/n的話,那麼就能夠使用3個雙 引號。並且使用3個雙引號還能夠在字符串中增長註釋,以下: s3 = """hello, #hoho, this is hello, 在3個雙引號的字符串內能夠有註釋哦 world, #hoho, this is world hahaha.""" 這就是3個雙引號和1個雙引號表示字符串的區別了,3個雙引號與1個單引號的區別也
29.a = "你好 ? ? 中國 ?",去除多餘空格只留一個空格。
a = "你好 ? ? 中國 ?" s1=a[:3]+a[3:].replace(" ","") s2=a[:7].replace(" ","")+a[7:] s3=a[:4].replace(" ","")+a[4:6]+a[6:].replace(" ","") s4=a[:6].replace(" ","")+a[6:8]+a[8:].replace(" ","")
列表
30.已知 AList = [1,2,3,1,2],對 AList 列表元素去重,寫出具體過程。
AList=[1,2,3,1,2] BList=[] for i in AList: if i not in BList: BList.append(i) print(BList)