python小測驗 python 小測驗複習

2021-10-11 12:57:42 字數 1556 閱讀 5030

str:

常用操作方法:

1)首字母大寫,其他字母小寫:

s = 'dasda'

s1 = s. capitalize()  print(s1)

2)居中center

s = 'asdad'

s1 = s.center(30,'***')  print(s1)

3)大小寫翻轉

s1 = s.swapcase()

print(s1)

4)每個單詞首字母大寫(非字母隔開)

s = 'dada das*ada2adad'

s1 = s.title()

print(s1)

5)判斷以什麼開頭,以什麼結尾 startswith,endswith

s1 = s.startswith('任意')

或者s1= s.startswith('老男孩')

6)去除首尾空格,換行符,tab

s1 = s.strip()   去除左邊lstrip() 右邊rstrip()

7)通過元素找索引:find index

s.find('')   s.find('',數)-->從『數』開始找

count:尋找元素出現的次數 s.count('')

8)split 分割 str-->list

s ='alex wusir taibai'

print(s.split())   --->['alex','wusir','taibai']

9)format

第一種:

s = '我叫{},今年{},愛好{}'.format('mt',15,'muniu')

print(s)

第二種:'我叫,今年,愛好,我依然叫'.format('mt',15,'mniu')

print(s)

第三種:鍵值對

s = '我叫,今年,愛好'.format(age=15,name='mt',hobby='悶')

print(s)

10)s.isalnum()   字串由字母或者數字組成

s.isalpha()   字串只由字母組成

s.isdigit()    字串只由數字組成

if s. isdigit():

+結果list l列表:

2:insert按索引插入

3:extend迭代加

4.pop按索引刪:

pop()預設刪最後乙個 有返回值

5.remove:按元素刪

6.clear:清空列表內容 list.clear()

7.del:1)刪除列表

del+列表

2)切片刪:

del 列表[數:數:數]

8.改:按索引改

list[索引] = '元素'

按切片改:

9.查:for迴圈查:

其他操作方法:

sort排序:

從小到大:list.sort()

從大到小:list.sort(reverse=true)

翻**list.reverse()

len 長度

count計數

python小測驗3 python基礎小測試

def is int int num check whether int num is integer if isinstance int num,str 判斷是否為字串型別 if int num.isdigit return true else return false elif isinstan...

python小測驗6 Python小記6

8.4 迴圈計算 程式中,利用迴圈流程,進行重複計算。例項演示 利用迴圈以及賦值運算等,計算1 100的和。按照數學等差數列公式,可以輕鬆得出 和 s 1 100 100 2,但是現在我們需要使用程式迴圈,進行原始的方法計算,s 1 2 3 4 100 0 100求和 定義計數器,初始化 i 1 定...

python第八周小測驗 Python小測試 9

精品 第八周1.在python 3.4 中,下列哪些是集合的有效表示方法?set set 1,2,3 2.假設s和 t表示已經定義好的兩個集合,x是 s集合中的某個元素,問以下哪些方法可以改變集合 s的值?s.symmetric difference update t s.intersection ...