python 中int ,float,字符串的操作


int 的功能

 

int:
1:bit_length(二進制位數的方法)返回當前數字占用的最小位數
2:__abs__ 返回絕對值,先創建一個數字對象,再調用.abs對象 abs(-10)
3:執行加運算是創建對象,調用方法,得出結果;
4:compare比較;
5:bool 向bool的轉換;
6:divmod 得到除數和余數
7:equal 判斷是否相等;__eq__()
8:float 轉換成float , 再內存中新創建一個float對象;
9:__floordiv__   地板除
10:#其實是與5//6相同
11:ge>=
12:gt <=
13:hash
14:index
15:__init__:構造方法
16:__invert__ :位運算
16:__or__ 或與|相同
17:__pow__ 冪運算
18:radd
19:rdivmod: 加上r是從右向左;(也就是運算順序不一樣)

重點是:abs divmod(商,余數) add

int里面基本都用不到,上面的一些方法都安置都語法層了;或者放到內置方法里面了;

float 與int 基本一致的;

在任何語言中80%都是對字符串的操作

字符串的操作

 

1:contains方法 (包含方法)
2:format 字符串格式化
3:getattribute 反射的時會用到;
4:getitem  字典里面有getitem setitem等方法;類似於:dict['k1']就是在調用getitem方法;
5:capitalize 首字母大寫
6:casefold 首字母小寫
7:center  居中在兩邊填上空格,若加上參數就是填充相應的字符 center(20,'*)
8:count:計算字符(或者是子序列)出現的次數; count(self ,sub ,start,end) 子序列,起始位置,開始位置,結束位置
9:encode 編碼:如:python3不用自己寫轉unicode的過程,但是內部一定是經歷了這一個過程的;如:str.encode('gbk')
10:endswith() 以什么結尾(一般如range(0,3)這樣的都是顧首不顧尾的)
11:startswith() 以什么開頭
12:expandtabs :把一個tab轉換成8個空格
13:find :返回所在索引的位置(如果是返回-1表示未找到;可以設置起始位置和結束位置
14:index 未找到會拋異常報錯,而find返回-1
15:format  做格式化的  name ='alex{0}'' ,result =name.format('sb','eric'),字符串的拼接;
16:isnumeric 是否是數字;
17:istitle:是否是標題
18:islower  isupper 是否是大小寫
19:join 字符串的拼接,如取出list中的數然后把他拼接起來;
20:ljust rjust
21:lstrip 去掉左邊的空格
22:translate 和maketrans 做對應的表;
23:partition :做字符串的分割 name = 'bluesliissb' partition('is') 通過is分割;
24:replace 替代  replace(‘a','o',1);把原來的a換成o,轉換1個;
25:rfind :就是方向反了
26:split :指定字符分割字符串
27:splitline: 根據換行符進行分割字符串返回一個list列表;
28:strip 取出兩邊空格
29:所有大小轉換成大寫,小寫轉換成大寫:swapcase
30:zfill:與ljust和rjust可以定制出來

split replace join find 常用的有這些

代碼如下:

a = 'bluesli'
print(a.__contains__('i'))
print(a.upper())
print(a.lower())
print(a.startswith('b',0,5))

b='bluesli{name}{id}'
c='bluesli{0}'
print(c.format('xx'))
print(a.format(name='name',id='id'))

print(a.endswith('i'))

print(a.casefold())
print(a.capitalize())

print(a.count('l',0,7))

print(a.encode('gbk'))

print(a.title())

print(a.center(20,'*'))

d = 'bludes'
print(d.expandtabs())

print(a.find('i'))
print(a.index('i'))

print('.'.join(a))

print(d.rjust(50,'0'))

print(a.split('l'))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM