赞
踩
本文重点放在常用函数、方法的总结,主要起“字典”的作用,忘了有啥方法可以查看此文速查,因此内容上做了些减法。结合Python语法基础来看可能效果会好一些。
x,y,z为某个数字;list为列表
- abs(x)
- divmod(x, y)
- pow(x, y)
- pow(x, y, z)
- round(x)
- round(x, d)
- min(list)
- max(list)
- sum(list)
- int(x)
- float(x)
- complex(x)
string为某个字符串;num为某个数字;firstNum为第一个数字;lastNum为最后一个数字;step为步长;u为unicode编码字符;old为老的子串;new为新的子串;sep为分隔的子串规则;fillchar为填充的字符;chars为字符;
字符串的操作方法:
- string.title()
- string.lower()
- string.upper()
- string.replace(old, new)
- string.center(num)
- string.center(num, fillchar)
- string.rstrip()
- string.lstrip()
- string.strip()
- string.strip(chars)
- new.join(string)
字符串的获取功能:
- string.count('xxx')
- string.split()
- string.split(sep)
- len(string)
- chr(u)
- ord(x)
其他类型转字符串的方法:
- str(num)
- hex(num)
- oct(num)
字符串切片(同列表切片):
- string[firstNum: lastNum: step]
- string[::-1]
字符串的格式化:
- "{} {}".format("hello", "world") :不设置指定位置,按默认顺序输出 hello world;
- "{1} {0} {1}".format("hello", "world") :设置指定位置,输出 world hello world;
- format函数传入对象:
- class AssignValue(object):
- def __init__(self, value):
- self.value = value
- my_value = AssignValue(6)
- print('value 为: {0.value}'.format(my_value)) # "0" 是可选的
数字的格式化:
- {字符串模板}.format(用逗号分隔的字符串参数) :使用字符串模板格式化字符串参数;
- format格式化函数的字符串模板,如下图所示
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。