
經過自己面試的經驗,我幫大家對python的知識點進行歸類,讓大家能在最短時間內掌握核心知識點,更高效的搞定 Python 面試!
今天我整理了關于“資料結構”和“字串”的面試題,大家一起試試看自己的熟練程度吧
1 列舉 Python 中的基本資料型別?
答: Python3 中有六個標準的資料型別:
字串(String)、
數字(Digit)、
串列(List)、
元組(Tuple)、
集合(Sets)、
字典(Dictionary),
2. 如何區別可變資料型別和不可變資料型別?
答: 從物件記憶體地址方向來說
可變資料型別:在記憶體地址不變的情況下,值可改變(串列和字典是可變型別,但是字典中的 key 值必須是不可變型別)
不可變資料型別:記憶體改變,值也跟著改變,(數字,字串,布爾型別,都是不可變型別)可以通過 id() 方法進行記憶體地址的檢測,
3.字串大小寫問題關于英文字串的大小寫轉換問題,可以通過幾個函式實作?
答:四種
(1)首字母大寫
a ='hello,zHong yUan GoNg!!'
print(a.title())
(2)全部大寫
print(a.upper())
(3)全部小寫
print(a.lower())
(4)首個單詞的首字母大寫
print(a.capitalize())
輸出結果為:
Hello,Zhong Yuan Gong!!
HELLO,ZHONG YUAN GONG!!
hello,zhong yuan gong!!
Hello,zhong yuan gong!!
4. 如何檢測字串中只含有數字?
答:可以通過 isdigit 方法,例子如下
s1 = "12223".isdigit()
print(s1)
s2 = "12223a".isdigit()
print(s2)
結果如下:
#True
#False
5. 將字串"ilovechina"進行反轉的方法寫出來
答:s1 = "ilovechina"[::-1] print(s1)
6. Python 中的字串格式化方式你知道哪些?
答:%s,format,fstring(Python3.6 開始才支持,現在推薦的寫法)
例子如下:
(1)通過位置格式化
print('hello, '.format('zhong', 'yuan', 'gong'))
(2)通過key填充
print('hello,,my name is !!'.format(name='tom', self='sir'))
(3)通過陣列的下標填充
n=['tom', 'sir']
print('hello,,my name is !!'.format(n=1))
(4)通過字典的key填充,鍵名不加引號
m={'name': 'tom', 'self': 'sir'}
print('hello,,my name is !!'.format(m=m))
上面輸出結果都是:
hello,tom,my name is sir!!
7.有一個字串開頭和末尾都有空格,比如“ adabdw ”,要求寫一個函式把這個字串的前后空格都去掉,
答:因為題目要是寫一個函式所以我們不能直接使用 strip,不過我們可以把它封裝到函式啊
def strip_function(s1):
return s1.strip()
s1 = " adabdw "
print(strip_function(s1))
8.說出你知道能洗掉字串中的空格集中函式
答:c =' hello world !!! '
(1)去掉字串開頭和末尾的空格
print(c.strip())
(2)去掉字串左邊的空格
print(c.lstrip())
(3)去掉字串右邊的空格
print(c.rstrip())
(4)去掉字串中所有的空格
print(c.replace(' ',''))
輸出依次為:
hello world !!!
hello world !!!
hello world !!!
helloworld!!!
注意:這里不要把strip函式和split函式搞混了,前者是洗掉字串中指定的字符,默認為空格,后者是用指定的字符分割字串,默認也是空格
9. 一個編碼為 GBK 的字串 s,要將其轉成 UTF-8 編碼的字串,應如何操作?
答:
#轉換字串編碼
s='hello,zhongyuan university,你很好!'
print(s.encode('utf-8'))
10.單引號、雙引號、三引號的區別?
答:單引號和雙引號是等效的,如果要換行,需要符號(\),三引號則可以直接換行,并且可以包含注釋
如果要表示Let’s go 這個字串
單引號:s4 = ‘Let\’s go’
雙引號:s5 = “Let’s go”
s6 = ‘I realy like“python”!’
這就是單引號和雙引號都可以表示字串的原因了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/153382.html
標籤:Python
