对于python而言,一切事物都是对象,对象是基于类创建的,对象继承了类的属性,方法等特性

  一.int

    首先我们来查看一下int包含了哪些函数

    

# python3.x
dir(int)
# ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes'] # python 2.x
dir(int)
# ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
# __abs__()  绝对值输出
num = 1
result = num.__abs__() print(result) num = -1
result = num.__abs__()
print(result)

__abs__ 绝对值

 num = -1
result = num.__add__(2) print(result) # 打印结果将输出 1

__add__ 加法

 num = 5
result = num.__and__(2)
print(result) #打印输出为0
# 0 0 0 0 0 1 0 1 5
# 0 0 0 0 0 0 1 0 2
#相同位为1则为1,由于没有相同位,所以5 & 2结果为0

__and__ 与&运算

 # 以下结果输出都是True
num = 11
print(num.__bool__()) # True num = -11
print(num.__bool__()) #True # 以下结果输出都是 False
num = 0
print(num.__bool__()) #False num = None
num = False
print (num.__bool__()) # False

__bool__ 布尔值

#通过divmod函数可以实现将一个int类型对象除以另一个int对象得到一个两个元素的列表,
#列表左边为除尽取整的值,第二个元素为取模的余数 num = 9
result = num.__divmod__(2)
print(result) #输出(4,1)

__divmod__ 除法取整取模

 num = 2
result = num.__eq__(3)
print(result) #打印结果为False
#2 == 3 结果为假 result = num.__eq__(2)
print(result)
#打印结果为True
# 2 == 2 结果为真

__eq__ ==比较运算符

 num = 9
print(num.__float__()) #打印结果为 9.0

__float__ 转换为浮点数

 num = int(181)
result = num.__floordiv__(9)
print(result) #打印输出20
#地板除 //取整

__floordiv__地板除//

num = int(181)
result = num.__getattribute__("bit_length")
print(result) #打印输出 <built-in method bit_length of int object at 0x100275020>
#说明该数据类型num存在bit_length这个属性,可以用于判断对象是否拥有某种属性

__getattribute__获取对象属性

 num = int(181)
print(num.__ge__(111))
#打印输出结果为True
#因为181大于111,所以结果为真,该属性用于判断大于等于该属性自身的方法,结果将返回真,否则为假

__ge__ 比较运算>=

 num = 181
print(int.__invert__(num))
#打印输出-182 num = -180
print(int.__invert__(num))
#打印输出179 num = -181
print(int.__invert__(num))
#打印输出180

__invert__ 非~运算

 num = -181
result = num.__le__(111)
print(result)
#打印输出结果为True
#当传人参数与对象本身相比较,只要对象小于或者等于传人的参数,则结果为真,否则为假

__le__ 小于等于

 num = -181

 result = num.__lshift__(1)
print(result)
#打印输出结果为-362 ,即-181 *( 2**1) result = num.__lshift__(2)
print(result)
#打印输出结果为-724 ,即-181*(2**2) #当传入参数大于等于0时且对象本身不能为0,首先参数本身为2的指数幂运算,然后再与对象本身相乘结果则为左移最终结果

__lshift__左移运算

 num = -181
print(num.__lt__(11)) #打印输出结果为True #凡是对象比传入的参数小,则结果为真,否则结果为假

__lt__小于

  num = -181
print(num.__mod__(3)) #打印输出结果为2,因为-181除以3等于60,余数为2,所以结果为2

__mod__取模运算

 num = 181
print(num.__mul__(2)) #打印输出结果为362,即181*2的结果

__mul__ 乘法运算

 num = -181
print(int.__neg__(num)) #打印结果为181,即-(-181),结果为181

__neg__一元运算减法

 num = 181
print(num.__ne__(181))
#打印结果为False print(num.__ne__(11))
#打印结果为True #凡是传入参数与对象本身不相等,则结果为真,否则为假

__ne__ 不等于比较

 num = 18
print(num.__or__(7)) #打印输出结果为23
# 0 0 0 1 0 0 1 0 18
# 0 0 0 0 0 1 1 1 7
# 0 0 0 1 0 1 1 1 23
位的或运算,凡是相同位有一位为真,即为1,则结果为真,即1,然后所以最终结果为23

__or__ 或|运算

 num = 9
print(num.__pow__(2))
#打印输出结果为81,即9**2

__pow__ 幂运算

 num = 6
print(num.__rdivmod__(3))
#返回结果(0,3) 左边为余数,右边为整除的结果

__rdivmod__ 与divmod返回的结果相反

 #python 2.7
num = 1
print(num.__sizeof__())
#打印输出结果为24个字节,说明一个int类型默认就在内存中占用了24个字节大小 #python3.5
num = 1
print(num.__sizeof__())
#打印输出结果为28个字节,说明一个int类型数据默认在内存中占用了24个字节大小

__sizeof__ 计算数据类型占用内存大小

 num = int(1111)
result = num.__str__()
print(type(result)) #打印输出结果为<class 'str'>
#将int类型转换为str数据类型

__str__ int转换成str

 num = int(9)
print(num.__sub__(2)) #打印输出结果为7
#对象本身减去传入参数,得到最终的返回值

__sub__ 减法运算

 num = 11
print(num.__truediv__(3)) #打印输出结果为3.6666666666666665
#返回的数据类型为float,浮点型

__truediv__ 真除

 num = 10
print(num.__xor__(6)) # 0 0 0 0 1 0 1 0 10
# 0 0 0 0 0 1 1 0 6
# 0 0 0 0 1 1 0 0 12 #同位比较,都是0则为假,都是1则为假,一真一假为真

__xor__ 异或^运算

 num = 5
print(num.bit_length())
#打印输出结果为3 # 0 0 0 0 0 1 0 1 #长度为3位

bit_length 显示数据所占位长度

 num = 2.3 - 2.5j
result = num.real #复数的实部
print(result) #打印输出2.3
result = num.imag #复数的虚部
print(result) #打印输出2.5j result = num.conjugate() #返回该复数的共轭复数
print(result) #打印输出(2.3+2.5j)

conjugate

 num = 5
print(num.__format__(""))
#表示5前面讲话有20个空格

__format__ 格式化输出

 print(int.from_bytes(bytes=b'', byteorder='little')

 #打印输出 49  ,即将字符1转换为十进制

from_bytes 字符转换十进制

 num = 2
result = num.to_bytes(5,byteorder='little')
print(result)
#打印输出b'\x02\x00\x00\x00\x00'
for i in result:
print(i) #打印输出2\n0\n0\n0\n0
#\n表示回车

to_bytes int转换为字节

  二.str

 #python3.5
dir(str)
#['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] #python2.7
dir(str)
#['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
 strA = "hello"
print(strA.__add__(" world"))
#输出hello world

__add__ 字符串拼接

 strA = "hello"
print(strA.__contains__("h"))
#True print(strA.__contains__('hex'))
#False

__contains__ 包含判断

 strA = "hello"
print(strA.__eq__('hello'))
#True print(strA.__eq__('hellq'))
#False

__eq__ 字符串==比较

 strA = "hello"
print(strA.__getattribute__('__add__'))
#<method-wrapper '__add__' of str object at 0x101d78730> #判断对象是否包含传入参数的属性

__getattribute__获取对象属性

 strA = "hello"
print(strA.__getitem__(0))
#输出下标为0的字符 h
#超出下标会报错的

__getitem__获取对应字符

 strA = "hello"
print(strA.__getnewargs__())
#打印输出 ('hello',)
#将字符类型转换为元组方式输出

__getnewargs__转换成元组

 strA = "hello"
print(strA.__ge__('HELLO'))
print(strA.__ge__('Hello'))
print(strA.__ge__('hello'))
#以上结果都为True,
print(strA.__ge__('hellq'))
#以上结果为假

__ge__ 字符串比较

 strA = 'Hello'
print(strA.__gt__('HellO'))
#打印输出True #字符串比较的是传入的参数每个字符首先得包含对象,其次如果字符串之间比较,大写比小写大,,如果传入参数都为大写,且对象也都为大写,那么结果为假,字符串比较首先比较的是字符串是否相同,不相同则为假,再次每个字符进行比较,只要前面有一位大于对方,则不继续比较了 #比如 HelLo与HEllo,首先两个字符串都是一样的,然后再比较第一位,第一位也一样,再比较第二位,大写比小写大,所以第二个字符串大,就不会继续比较下去了

__gt__ 字符串大于判断

 strA = "hello"
print(strA.__hash__())
#-7842000111924627789

__hash__ 生成一个临时的hash值

 strA = "hello"
result = strA.__iter__()
for i in result:
print(i) #打印输出
#h
#e
#l
#l
#o

__iter__ 字符串迭代

 strA = 'hello'
print(strA.__len__()) #打印输出结果为5

__len__ 判断字符串长度

 strA = 'Hello'
print(strA.__le__('ello'))
#True #字符串小于运算比较,先比较对象是否包含传入参数,当包含则再比较相同位的字母,大小字母比小写字母大,当前面有一位比较出谁大谁小了,则不再继续比下去了

__le__小于等于

 strA = 'hello'
print(strA.__lt__('ello'))
#True #字符串小于比较与小于等于比较类似,唯一一点是小于比较时,对象与传入的参数大小写不能完全一样

__lt__ 小于

 strA = 'hello'
print(strA.__mul__(3))
#hellohellohello #打印结果将输出三个hello

__mul__ 乘法运算

 strA = "hello"

 print(strA.__ne__('HEllo'))
#True #字符串不等于运算比较,凡是对象与传入参数只要有一个字母大小写不一样则为真,否则为假

__ne__ 不等于比较

 strA = "HELLO"
print(strA.zfill(6))
#0HELLO #当传入的参数长度比对象长度大时,多余的长度则以0进行填充

zfill 以0填充

 strA = "hELlo1112123"
print(strA.upper())
#HELLO #将所有的字母转换为大写

upper 字母转换大写

 print("hello world".title())

 #Hello World

 #每个单词首字母大写输出,且单词的第二位后面都会变成小写,如helLO,最终会格式化为Hello

title 标题

 print("hEllO".swapcase())
#HeLLo #将原来的大小字母转换成小写字母,小写转换成大小字母

swapcase 大小写转换

 print("    hello   world   ".strip())
#hello world
#将字符串两边的空格去掉

strip 去除字符串两边的空格

 print("hello".startswith('h'))
#True print("hello".startswith('h',1))
#False #startswith这个函数可以指定起始位置进行判断字符是否存在

startswith 字符串是否存在该字符

 print("hello\nworld".splitlines())
#['hello','world'] #splitlines默认以\n换行符进行分割字符,最终返回一个列表

splitlines 以换行符分割字符串

 print("hello world".split())
#['hello','world'] print("hello world".split('\n'))
#['hello world',] #默认以空格分割字符串,可以指定分隔符

split 默认以空格分割字符

 print("  hello  world    ".rstrip())

 #  hello  world
#打印将会把world后面的空格去除

rstrip 去除右边的空格

 print("hello world".rpartition('he'))
#('', 'he', 'llo world ')
#只返回传入参数且存在字符串里的字符然后组合成一个新的元组

rpartition 返回字符串的一部分

 print("hello world".rjust(20))
# hello world
#默认以空格填充,从左到最后一个单词d结尾一个长度为20,也就是说h前面有9个空格 print("hello world".rjust(20,'+'))
#+++++++++hello world
#这里以‘+’填充,对比上面,可以看的更具体,前面有9个+被用来填充

rjust 向右偏移

 print("hello world".rindex('wo'))
#
#通过查找字符串'wo'获取该字符串在hello world 里面的下标位置,这里从左往右数,第七个位置,字符串的下标默认从0开始,所以返回6
#当找不到时则抛出异常

rindex 查找下标

 print("hello world".rindex('wo'))
#
#通过查找字符串'wo'获取该字符串在hello world 里面的下标位置,这里从左往右数,第七个位置,字符串的下标默认从0开始,所以返回6
#当找不到时则抛出异常

rindex 查找下标

 print("hello world".rindex('wo'))
#
#通过查找字符串'wo'获取该字符串在hello world 里面的下标位置,这里从左往右数,第七个位置,字符串的下标默认从0开始,所以返回6
#当找不到时则抛出异常

rindex 查找下标

 print("hello world".rindex('wo'))
#
#通过查找字符串'wo'获取该字符串在hello world 里面的下标位置,这里从左往右数,第七个位置,字符串的下标默认从0开始,所以返回6
#当找不到时则抛出异常

rindex 查找下标

 print("hello world".rindex('wo'))
#
#通过查找字符串'wo'获取该字符串在hello world 里面的下标位置,这里从左往右数,第七个位置,字符串的下标默认从0开始,所以返回6
#当找不到时则抛出异常

rindex 查找下标

 print("hello world".rindex('wo'))
#
#通过查找字符串'wo'获取该字符串在hello world 里面的下标位置,这里从左往右数,第七个位置,字符串的下标默认从0开始,所以返回6
#当找不到时则抛出异常

rindex 查找下标

 strA = 'hello 123'
table1 = str.maketrans('','我很好')
print(strA.translate(table1))
#hello 我很好 #将字符串里面的123通过table进行翻译成对应的值,table1的123长度必须和‘我很好长度对应’ strA = 'hello 12'
table1 = str.maketrans('','我很好')
print(strA.translate(table1))
#hello 我很

translate 翻译

 print("hello".rfind('e'))
# print("hello".rfind('ee'))
#-1 如果找到,则结果为对应的下标,否则返回-1

rfind 从左到右查找

 print('hello world'.replace('e','o'))
#hollo world
#将字符串里面所有的e替换成o,区分大小写

replace 字符串替换

 print('hello world'.rpartition('el'))
#('h', 'el', 'lo world')
#效果与rpartition相似

partition 截取字符串

 table1 = str.maketrans('','我很好')
print(table1)
#{49: 25105, 50: 24456, 51: 22909}
#首先传入的必须是两个参数,且长度相等
#返回结果将是一个字典类型,每一个字符串将会映射到第二个参数的相同位置的字符串上,
#当这里存在三个参数时,第三个参数必须是一个字符串类型,且整个字符串将被映射成None strA = 'hello 1233飒飒'
table1 = str.maketrans('','我很好','飒飒')
print(strA.translate(table1))
print(table1) #以下为输出结果
#hello 我很好好
#{49: 25105, 50: 24456, 51: 22909, 39122: None} #这个字典的值将被映射成unicode值,如49表示unicode的1

maketrans 翻译表

 table1 = str.maketrans('','我很好')
print(table1)
#{49: 25105, 50: 24456, 51: 22909}
#首先传入的必须是两个参数,且长度相等
#返回结果将是一个字典类型,每一个字符串将会映射到第二个参数的相同位置的字符串上,
#当这里存在三个参数时,第三个参数必须是一个字符串类型,且整个字符串将被映射成None strA = 'hello 1233飒飒'
table1 = str.maketrans('','我很好','飒飒')
print(strA.translate(table1))
print(table1) #以下为输出结果
#hello 我很好好
#{49: 25105, 50: 24456, 51: 22909, 39122: None} #这个字典的值将被映射成unicode值,如49表示unicode的1

maketrans 翻译表

 table1 = str.maketrans('','我很好')
print(table1)
#{49: 25105, 50: 24456, 51: 22909}
#首先传入的必须是两个参数,且长度相等
#返回结果将是一个字典类型,每一个字符串将会映射到第二个参数的相同位置的字符串上,
#当这里存在三个参数时,第三个参数必须是一个字符串类型,且整个字符串将被映射成None strA = 'hello 1233飒飒'
table1 = str.maketrans('','我很好','飒飒')
print(strA.translate(table1))
print(table1) #以下为输出结果
#hello 我很好好
#{49: 25105, 50: 24456, 51: 22909, 39122: None} #这个字典的值将被映射成unicode值,如49表示unicode的1

maketrans 翻译表

 table1 = str.maketrans('','我很好')
print(table1)
#{49: 25105, 50: 24456, 51: 22909}
#首先传入的必须是两个参数,且长度相等
#返回结果将是一个字典类型,每一个字符串将会映射到第二个参数的相同位置的字符串上,
#当这里存在三个参数时,第三个参数必须是一个字符串类型,且整个字符串将被映射成None strA = 'hello 1233飒飒'
table1 = str.maketrans('','我很好','飒飒')
print(strA.translate(table1))
print(table1) #以下为输出结果
#hello 我很好好
#{49: 25105, 50: 24456, 51: 22909, 39122: None} #这个字典的值将被映射成unicode值,如49表示unicode的1

maketrans 翻译表

 print("   hello world   ".lstrip())
#hello world
将hello左边空格去除

lstrip 去除左边的空格

 print("HELLo22".lower())
#hello22
#将所有字母转换为小写

lower 转换小写

 print("hello world".ljust(20,'+'))
#hello world+++++++++
#从右向左开始进行填充,总长度为20

ljust 右填充

print('+'.join(('hello','world')))
#hello+world
#通过一个字符串去与join里面的一个迭代器里的字符串进行联结生存一个新的字符串

join 生存一个字符串

print('Hello'.isupper())
print('HELLO1'.isupper())
#False
#True #判断所有的字母是否都是大小,是则返回真,否则假

isupper 是否全部大小

 print('Hello'.istitle())
print('Hello world'.istitle())
#True
#False
#判断每个单词首字母是否大写,是则为真,否则为假

istitle 是否是标题

 print(' hello'.isspace())
print(' '.isspace())
#False
#True
#判断内容是否为空格

isspace 是否是空格

 print('hello world'.isprintable())
print('\n'.isprintable())
#True
#False
#由于换行符是特殊字符,不可见,所以不能被打印,结果为假

issprintable 是否可以被打印

 print(''.isnumeric())
print('壹'.isnumeric())
print('1q'.isnumeric())
#True
#True
#False
#True包含unicode数字,全角数字(双字节),罗马数字,汉字数字

isnumeric 是否是数字

 print('Hello'.islower())
print('hello'.islower())
#False
#True
#判断字母是不是都是小写,是则为真,否则为假

islower 是否是小写

 print('def'.isidentifier())
print('hello'.isidentifier())
print('2a2'.isidentifier())
#True
#True
#False #用来检测标识符是否可用,也就是说这个名字能不能用来作为变量名,是否符合命名规范,如果符合则为真
#通常会结合keyword.iskeyword()这个方法去在做判断是否是关键字,防止因命名不规范导致某些内置功能不可用

isidentifier

 print('hello'.isdigit())
print('111e'.isdigit())
print('壹'.isdigit())
print(''.isdigit()) #False
#False
#False
#True #unicode数字,全角数字(双字节),byte数字,罗马数字都为真

isdigit 是否是数字

 print(''.isdecimal())
print('壹'.isdecimal())
print('11d'.isdecimal())
#
#True
#False
#False
#只有全部为unicode数字,全角数字(双字节),结果才为真

isdecimal 是否是数字

 print('hee'.isalpha())
print('Hello'.isalpha())
print(''.isalpha())
print('hhee1'.isalpha())
#True
#True
#False
#False
#当结果都是字母则为真,否则为假

isalpha 是否是字母

 print('hew11'.isalnum())
print('HHH'.isalnum())
print(''.isalnum())
print(' q '.isalnum())
print('!!@~d'.isalnum())
#True
#True
#True
#False
#False #当结果为任意数字或字母时,结果为真,其他字符为假

isalnum 是否为数字或字母

 print('hello'.index('e'))
print('hello'.index('el'))
print('hello'.index('el',1))
#
#
#
#通过查找制定的字符获取对应字符串的下标位置,可以指定起始位置,第3个事咧则表示从下标1开始查找,包括下标1的位置,如果指定end的结束位置,查找是不包括end的位置本身

index 通过字符查找下标

 print('hello'.find('h',0))
print('hello'.find('h',1))
#
#-1 #find是从下标0位置开始找起,包含开始的位置0,如果有结束的位置,不包含结束位置,查找到则显示具体下标位置,否则显示-1

find查找字符串下标

 print('hello{0}'.format(' world'))
print('hello{0}{1}'.format(' world',' python'))
print('hello{name}'.format(name=' world'))
#hello world
#hello world python
#hello world

format 格式化输出字符串

 print('hello\tworld'.expandtabs(tabsize=8))
#hello world 指定制表符长度为8

expandtabs 制表符长度

 print('hello'.endswith('lo',3))
#True
#判断结束字符是否为lo,默认从下标0开始查找,包含下标0的位置

endswith 判断结束字符

 print('我好'.encode())
print('hello'.encode())
# print('hela!~@@~!\xe2lo'.encode('gbk',errors='strict'))
print(b'\xe6\x88\x91\xe5\xa5\xbd'.decode('utf-8')) #b'\xe6\x88\x91\xe5\xa5\xbd'
#b'hello'
#我好 #将字符串进行编码,最终返回以b开头的编码格式

encode 编码

 print('heelloe'.count('e',1,2))
#
#表示从开始下标1包括下标1位置查找字符e,结束位置为下标2,不包括结束位置
#统计结果为1

count 统计相同的字符

 print('aaa'.center(22,'+'))
#+++++++++aaa++++++++++
#表示将字符aaa的位置显示在长度为22的中间位置,默认是空格方式填充,这里以+号填充方便演示效果,注意,由于22-3(字符本身3个长度),剩余的并不能整除,所以先整除的整数部分作为公共的填充内容,剩余的填充到末尾

center 中心显示

 print('hDasdd23ellAo'.casefold())
#hdasdd23ellao #将字符串里面所有的字母都转换为小写输出

casefold 字母转换小写

 print('hEello World'.capitalize())

 #Heello world
#这个方法会将整个字符串的第一个字母大写,其余都是小写输出,如果第一个字符串不是字母,则只将其余字母转换成小写

capitalize 首字母大写

  三.dict

 #python3.5
dir(dict)
#['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] #python2.x
dir(dict)
#['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
 food = {'':'apple','':'banana'}
print(food)
food.clear()
print(food) #{'2': 'banana', '1': 'apple'} 正常打印结果
#{} 调用字典函数clear打印结果

clear 清空字典

 food = {'':'apple','':'banana'}
newfood = food.copy()
print(newfood)
#{'1': 'apple', '2': 'banana'} 打印输出,

copy 浅拷贝字典

 food = {'':'apple','':'banana'}
print(food.fromkeys(('w'),('','')))
print(food) #{'w': ('2', '5')}
#{'2': 'banana', '1': 'apple'} #注意,这个操作并不会改变字典的数据值,仅仅是返回一个新字典

fromkeys 返回一个新字典

 food = {'':'apple','':'banana'}
print(food.get(''))
print(food.get(''))
print(food.get('','neworange'))
print(food.get('','neworange'))
print(food) #None 如果没有这个key将返回一个默认值none
#apple 如果能能查到key则显示对应的值
#neworange 如果查不到,则显示默认的值
#apple 如果能查到key,只显示key对应的值,否则使用默认的值
#{'1': 'apple', '2': 'banana'} get不会改变字典内容

get 获取字典值

 food = {'':'apple','':'banana'}
print(food.items()) #dict_items([('1', 'apple'), ('2', 'banana')]) 将字典的键存放在一个元组,对应的值也放在另一个元组里面,返回

items 获取字典的key,values

 food = {'':'apple','':'banana'}
print(food.keys()) #dict_keys(['2', '1'])

keys 以元组形式返回字典键

 food = {'':'apple','':'banana'}
result = food.pop('')
print(result)
print(food) #apple 将被删除的键对应的值返回
#{'2': 'banana'} 打印更新后的字典

pop 删除指定的键值对

 food = {'':'apple','':'banana'}
print(food.popitem())
print(food) #('1', 'apple') 随机删除键值对
#{'2': 'banana'} 返回删除后的字典

popitem 随机删除键值对

 food = {'':'apple','':'banana'}
print(food.setdefault('','orange'))
print(food) #orange 默认的值
#{'3': 'orange', '2': 'banana', '1': 'apple'} 打印字典

setdefault 设置默认的键值对

 food = {'':'apple','':'banana'}
goods = {'':'TV','':'Computer'}
print(food.update(goods))
print(food) #None
#{'2': 'banana', '1': 'TV', '22': 'Computer'} 如果存在对应的key则更新value,否则新增键值对

update 更新字典

 food = {'':'apple','':'banana'}
print(food.values()) #dict_values(['apple', 'banana'])

values 以列表形式返回字典的值

python基础学习二——第二天的更多相关文章

  1. Python基础学习二

    Python基础学习二 1.编码 utf-8编码:自动将英文保存为1个字符,中文3个字符.ASCll编码被囊括在内. unicode:将所有字符保存为2给字符,容纳了世界上所有的编码. 2.字符串内置 ...

  2. 【Python基础学习二】定义变量、判断、循环、函数基本语法

    先来一个愉快的Hello World吧,就是这么简单,不需要写标点符号,但是需要严格按照缩进关系,Python变量的作用域是靠tab来控制的. print("Hello World" ...

  3. python基础学习笔记第二天 内建方法(s t r)

    python的字符串内建函数 str.casefold()将字符串转换成小写,Unicode编码中凡是有对应的小写形式的,都会转换str.center()返回一个原字符串居中,并使用空格填充至长度 w ...

  4. python基础学习二 数据结构之list及相关基本操作

    list是py内置的一种数据类型,list就是列表的意思,list就是一种有序的数据集合,可以随时增加和删除list的元素. 生活中,比如我们要列出全班同学的名字,就可以用list来表示 >&g ...

  5. Python入门基础学习 二

    Python入门基础学习 二 猜数字小游戏进阶版 修改建议: 猜错的时候程序可以给出提示,告诉用户猜测的数字偏大还是偏小: 没运行一次程序只能猜测一次,应该提供多次机会给用户猜测: 每次运行程序,答案 ...

  6. 《python基础教程(第二版)》学习笔记 文件和素材(第11章)

    <python基础教程(第二版)>学习笔记 文件和素材(第11章) 打开文件:open(filename[,mode[,buffering]]) mode是读写文件的模式f=open(r' ...

  7. 《python基础教程(第二版)》学习笔记 类和对象(第7章)

    <python基础教程(第二版)>学习笔记 类和对象(第7章) 定义类class Person:    def setName(self,name):        self.name=n ...

  8. 《python基础教程(第二版)》学习笔记 函数(第6章)

    <python基础教程(第二版)>学习笔记 函数(第6章) 创建函数:def function_name(params):  block  return values 记录函数:def f ...

  9. 《python基础教程(第二版)》学习笔记 语句/循环/条件(第5章)

    <python基础教程(第二版)>学习笔记 语句/循环/条件(第5章) print 'AB', 123 ==> AB 123 # 插入了一个空格print 'AB', 'CD' == ...

随机推荐

  1. android开子线程避免出现main错误

    Runnable SonThread=new Runnable() { @Override public void run() { // TODO Auto-generated method stub ...

  2. hdu 4055 递推

    转自:http://blog.csdn.net/shiqi_614/article/details/7983298 题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果 ...

  3. 第七篇:创建一个SOUI的Hello World

    从0开始一个SOUI项目 1.环境配置 SOUI项目本质是一个基于Win32窗口的应用程序.因此首先我们可以从Win32窗口应用程序向导创建一个简单的Win32项目. 并在第3页选择“Window应用 ...

  4. 数据库字典 sql

    SELECT 表名=case when a.colorder=1 then d.name else '' end, 表说明=case when a.colorder=1 then isnull(f.v ...

  5. BNUOJ1067生成函数入门

    https://www.bnuoj.com/v3/problem_show.php?pid=1067

  6. How Kafka’s Storage Internals Work

    In this post I'm going to help you understand how Kafka stores its data. I've found understanding th ...

  7. Adobe Flash Media Server安装

    Flash Media Server(FMS)是一个流媒体服务器 使用 实时消息传送协议(RTMP),RTMP是一种未加密的TCP/IP协议,专门设计用来高速传送音频.视频和数据信息. 3.5版32位 ...

  8. json 入门(1)

    1.JSONObject介绍 JSONObject-lib包是一个beans,collections,maps,Java arrays和xml和JSON互相转换的包. 2.下载jar包 http:// ...

  9. 6.android加密解析

    编码.数字摘要.加密.解密 UrlEncoder /Urldecoder String str = "http://www.baidu.com?serach = 哈哈"; Stri ...

  10. poj1753 bfs+奇偶性减枝//状压搜索

    http://poj.org/problem?id=1753 题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右) ...