1. 程式人生 > 實用技巧 >python開發基礎(二)常用資料型別呼叫方法

python開發基礎(二)常用資料型別呼叫方法

 1 數字: int 
 2 
 3 int : 轉換,將字串轉化成數字
 4 num1 = '123'
 5 num2 = int (a)
 6 numadd = num2 +1000
 7 print(num2)
 8 
 9 num3 = 'a'
10 v = int (num3 , base = 16 ) # base==n ,base 表示進位制,base = 16 就是16進位制
11 print(v)
12 
13 
14 bit_length : 至少用幾個二進位制位表示
15 age = '18'
16 a = age.bit_length() #  該數字的二進位制位是多少
17 print
(a) 18 19 20 字串: str 21 22 callable # 首字母大寫 23 text = 'str' 24 b = text.callable() 25 print(b) 26 27 callable # 把所有的變成小寫 28 c = text.casefold() # 未知的對應關係也可以. 29 print(c) 30 d = text.lower() 31 print(d) 32 33 center # 設定寬度,並將內容居中 34 e = text.center(20,'#') # center 表示字串居中,列印輸出20個字元位,其他用'#'代替,代替符只能是一個字元
35 print(e) 36 37 count # 計算該字串中出現了多少次該字元 38 f = text.count('e',5) # 從開始到第5個字元,可以設定起始位置 和 結束位置 39 print(f) 40 41 ##### encode decode 42 43 endswith # 以XXX結尾(判斷) 44 g = text.endswith('a') # 判斷該字串是不是以'a'結尾 45 print(g) 46 startswith # 以XXX開頭(判斷) 47 48 find # 從開始往後找,找到第一個之後,獲取其位置 49 h = text.find('
a',5) # 從開始到第5個字元,可以設定起始位置 和 結束位置 50 51 format # 格式化,將字串中的佔位符替換成指定的值 52 text = 'i , an ,{name},age {a} ' 53 print(text) 54 j = text.format(name='XXX',a=18) # 從text中的{name}替換成'XXX' 55 56 text = 'i , an ,{0},age {1} ' 57 j0 = text.format('XXX',18) # 從text中的0替換成'XXX' 58 59 text = 'i , an ,{name},age {a} ' 60 k1 = text.format (name='XXX',a=28) 61 k2 = text.format ({'name':'XXX','age','18'}) 62 print(k1) 63 print(k2) 64 65 index # 和find 相同,如果找不到報錯 66 l = text.index('8') 67 68 isalnum # 判斷只能字串中只包括字母和數字 69 q = text.isalnum() 70 71 expandtabs # 斷句按位,空字元補全 72 w = text.expandtabs(2)