标签:tuple st3 元祖 div lex 一个 name 占位符 nbsp
1.数字整形
python3不管数字有多大都是int型,没有long类型
1>字符串转换为数字
s1 = "123" print(type(s1),s1) b = int(s1)#不加base默认转换为十进制 print(type(b),b) b += 1000
输出:
<class ‘str‘> 123
<class ‘int‘> 123
s1 = "0011" s2 = "a" print(type(s1),s1) b = int(s1,base=2)#二进制 c = int(s2,base=16)#十六进制 print(type(b),b) print(type(c),c)
输出:
<class ‘str‘> 0011
<class ‘int‘> 3
<class ‘int‘> 10
2>-bit_length()方法
age = 7 # 7 111 # 3 11 # 1 01 # 当前数字的二进制至少用n位表示 r = age.bit_length() print(r)# 3
2.字符串
str
#!/usr/bin/env python # -*- coding:utf-8 -*- test = ‘helLo‘ v = test.capitalize() # 首字母大写 print(v) # Hello v1 = test.casefold() # 所有变小写,更牛逼 print(v1) # hello v2 = test.lower() # 所有变小写 print(v2) # hello # 设置宽度,并将内容居中 # 20 代指总长度 # * 空白未知填充,一个字符,可有可无 v3 = test.center(20,‘*‘) print(v3) # *******helLo******** # 去字符串中寻找子序列出现的次数 v4 = test.count(‘l‘,4) print(v4) # 0 # 以什么什么结尾或开始,返回bool v5 = test.endswith(‘lo‘) v6 = test.startswith(‘h‘) print(v5) print(v6) # 从开始往后找,找到第一个之后,获取其位置 test1 = ‘hellohello‘ v7 = test1.find(‘oh‘, 4, 6) # 大于等于4,小于6 print(v7) # 4 # 格式化,将字符串中的占位符替换为指定的值 test2 = ‘i am {name}, age {a}‘ print(test2) # ‘i am {name}, age {a}‘ v8 = test2.format(name=‘Alex‘,a=18) print(v8) # i am Alex, age 18 test3 = ‘i am {0}, age {1}‘ print(test3) # ‘i am {0}, age {1}‘ v9 = test3.format(‘Alex‘,18) print(v9) # i am Alex, age 18 v10 = test2.format_map({"name":‘Alex‘,"a":18}) print(v10) # i am Alex, age 18 v11 = test3.index("am") print(v11) # 判断字符串是否只包含数字和字母 v12 = test2.isalnum() print(v12) # False
3.列表
list
4.元祖
tuple
5.字典
dict
6.布尔值
bool
标签:tuple st3 元祖 div lex 一个 name 占位符 nbsp
原文地址:https://www.cnblogs.com/xiangtingshen/p/10351237.html