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',)) 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'))