#python 字符串 # /user/sbin/py python # -*-coding:utf-8-*- test1 = "abc" v1 = test1.center(20 ,"*") #字符填充 v2 = test1.ljust(20 , "*") #原内容在左 v3 = test1.rjust(20 , "*") #原内容在右 print(v1) print(v2) print(v3) v4 = test1.islower() #判断是否全部小写 v5 = test1.isupper() #判断是否全部大写 print(v4,v5) test2 = " \na b c " vt1 =test2.lstrip() #去除左边的空格 (不仅可以去除空格还可以去除换行) print(vt1) vt2 = test2.rstrip() #去除右边的空格 print(vt2) vt3 = test2.strip() #去除左右两边空格 print(vt3) test3 = "abc" vt4 = test3.rstrip("c") #去除字符串 print(vt4) test4 = "abcd" test5 = "1234" m = str.maketrans(test4,test5) #将字符转换成对应的ascii码值,根据key value的形式映射起来 print(m) #{97: 49, 98: 50, 99: 51, 100: 52} test6="asdfsafdsaff" vs6 = test6.split("s") #字符串分割 print(vs6) #['a', 'df', 'afd', 'aff'] vs7 = test6.split("s",2) #指定分割几次 print(vs7) #split方法存在缺陷,做为分割字符的参数s 自己是取不到的 vs8 = test6.partition("s") #分割成三份,被分割字符占一份 print(vs8) test7 = "abc\ndef\njqk" ve1 = test7.splitlines() #根据换行符进行分割 print(ve1) ve2 = test7.splitlines(True) print(ve2)
python03 基本数据类型 string
最新推荐文章于 2022-12-19 15:07:01 发布