python基本数据类型-----int, str, list, tuple, dict, bool
数字 int
(1)int 转换:
a = "123" b = int(a) print(b)
输出: 123
num = "0011"
v = int(num, base=2)
print(v)
输出: 3
age = 5
r = age.bit.length()
print(r)
输出: 3 表示至少需要的二进制位数5是101
字符串 str
(1)center() 字符串居中
test = "alex"
v = test.center(20, "*")
print(v)
输出: ********alex********
(2)count() 某字符串出现的次数计数
test = "alexalexr"
v = test.count("ex", start, end)
print(v)
输出: 1 or 2 or 0 当设置不同的起始位置,会的到不同的结果。
(3)endswith()以什么结尾
test = "alex"
v = test.endswith('ex')
print(v)
输出: True
(4)find() 从开始找某字符串返回下标
test = "alexalex" v = test.find('ex', start, end) print(v)
输出: 2
(5)format() 占位符,将一个字符串中的占位符替换成指定的值
test = "i am {name},{age}" print(test) v = test.format(name = 'alex', age =19) print(v)
输出:
i am {name},{age}
i am alex,19
test = "i am {0},{1}" print(test) v = test.format('alex' ,19) print(v)
输出:
i am {0},{1}
i am alex,19
(6)format_map() 将一个字符串中的占位符替换成指定的值,传入的值:{"name" : 'alex', "a" : 19}
test = "i am {name}, age {a}"
print(test)
v = test.format_map({"name" : 'alex', "a" : 19})
print(v)
输出:
i am {name}, age {a}
i am alex, age 19
(7)isalum() 判断字符串中是否只是数字和字母
test01 = "123_nnjs" test02 = "123jjs" v1 = test01.isalum() v2 = test02.isalum() print(v1 ,v2)
输出: False ,True
(8)expandtabs(),断句20 test ="username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123" v = test.expandtabs(20) print(v) 输出: username email password laiying ying@q.com 123 laiying ying@q.com 123 laiying ying@q.com 123
(9) join() ,将字符串中的每一个元素按照指定分隔符进行拼接 test = "你是风儿我是沙" print(test) v = "_".join(test) print(v) 输出: 你是风儿我是沙 你_是_风_儿_我_是_沙
(10)ljust(), rjust()指定字符长度,指定用特定符号补充 test = "alex" v = test.ljust(20, "*") print(v) # 输出: # alex**************** test = "alex" v = test.rjust(20, "*") print(v) # 输出: # ****************alex
(11) low(), islow(),小写转换与判断 test = "Alex" v1 = test.islower() v2 = test.lower() print(v1, v2) # 输出:False alex
(12) upper(), isupper(),大写转换与判断 test = "Alex" v1 = test.isupper() v2 = test.upper() print(v1, v2) # 输出:False ALEX
(13) lstrip(), rstrip(), strip()默认去空格,\n,\t;加参数可以指定去除某个字符串 test = " alex " v1 = test.lstrip() # 去左边空格 v2 = test.rstrip() # 去右边空格 v3 = test.strip() # 去空格 print(v1,v2,v3) # 输出:alex
(14) partition() , rpartition(), split(), rsplit()以指定字符串分割 test = "testkstkawlked" v1 = test.partition("st") v2 = test.rpartition("st") v3 = test.split("st") v4 = test.rsplit("st") print(v1, type(v1)) # 输出: ('te', 'st', 'kstkawlked') <class 'tuple'> print(v2, type(v2)) # 输出:('testk', 'st', 'kawlked') <class 'tuple'> print(v3, type(v3)) # 输出:['te', 'k', 'kawlked'] <class 'list'> 没获取到分割的当前字符"st" print(v4, type(v4)) # 输出:['te', 'k', 'kawlked'] <class 'list'> """ 输出: ('te', 'st', 'kstkawlked') <class 'tuple'> ('testk', 'st', 'kawlked') <class 'tuple'> ['te', 'k', 'kawlked'] <class 'list'> 没获取到分割的当前字符"st" ['te', 'k', 'kawlked'] <class 'list'> """
(15)splitlines(), 参数True或False: 是否保留换行符
test = "lskdfie\nksjkd\nksdkfow" v = test.splitlines(True) v1 = test.splitlines(False) print(v, v1) # 输出: # ['lskdfie\n', 'ksjkd\n', 'ksdkfow'] # ['lskdfie', 'ksjkd', 'ksdkfow'] (16)索引,下标,切片, test = "ksielw" v = test[0:3] # 不包含index=3 v1 = test[0:-1] v2 = test[0:] print(test[0]) print(v) print(v1) print(v2) """ 输出: k ksi ksiel ksielw """ (17) replace(a1,a2,a3) 用a2替换a1, 默认替换完全,a3是替换的个数 test = "alexalexalex" v = test.replace("ex","ss") v1 = test.replace("ex","ss",2) print(v) print(v1) # 输出: alssalssalss, alssalssalex (18)range(0, 100, 5)获得0~99的数字,5是步长r = range(0,10) print(r) # 输出:range(0, 10) for i in r: print(str(i)+"-", end="") """ 输出: 0-1-2-3-4-5-6-7-8-9- """ test = "alex" l = range(0, len(test)) for i in l: print(i, test[i]) """ 输出: 0 a 1 l 2 e 3 x """
(19)isnumeric(),isdigit(),isdecimal(),数字判断,经常用 isdecimal()
test1 = "二" test2 = "②"
test3 = "2"
v1 = test1.isnumeric()
v2= test2.isnumeric()
v3 = test3.isnumeric()
print (v1,v2,v3)
输出: True True True
v1 = test1.isdigit()
v2 = test2.isdigit()
v3 = test3.isdigit()
print (v1,v2,v3)
输出:
False True True
v1 = test1.isdecimal()
v2 = test2.isdecimal()
v3 = test3.isdecimal()
print (v1,v2,v3)
输出:
False False True
(20)isprintable()是否存在不可显示的字符
# \t 制表符
# \n 换行
test = "oiuas\tdfkj"
v = test.isprintable()
print(v)
输出:False

浙公网安备 33010602011771号