int及str方法的使用

it2023-12-03  65

python 类型有:整形int,字符串str,列表list,元祖tuple,字典dict,布尔值bool a ='10'print(type(a),a)b=int (a) #将字符串转为int类型,使用type可以查看类型print(type(b),b)str:1.count() 去字符串中寻找,寻找子序列的出现次数,如下 name="root"text=name.count("r")print(text) 结果:12.capitalize() 首字母大写 name ="root"text=name.capitalize()print(text) 输出结果为Root 3.casefold() 所有字母小写 name ="ROOT"text=name.casefold()print(text) 输出结果为root4.center()内容居中 name ="ROOT"text=name.center(10,'"')# 10代表总长度 print(text) 输出结果为:"""ROOT""" 5.startswith以什么开始,endswith已什么结束,返回为true或flase name ="ROOT"text=name.startswith("T")text1=name.endswith("T")print(text) #输出结果为flaseprint(text1)#输出结果为true6.*****find 从开始往后找,找到第一个之后,获取其未知 test = "alexalex"# 未找到 -1 v = test.find('ex') print(v) #输出结果为27.****format 格式化,将一个字符串中的占位符替换为指定的值test = 'i am {name}, age {a}'print(test)v = test.format(name='alex',a=19)print(v)test = 'i am {0}, age {1}'print(test)v = test.format('alex',19)print(v)# 格式化,传入的值 {"name": 'alex', "a": 19}test = 'i am {name}, age {a}'v1 = test.format(name='df',a=10)v2 = test.format_map({"name": 'alex', "a": 19})# 字符串中是否只包含 字母和数字test = "123"v = test.isalnum()print(v) # 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) #结果以表格形式输出 # 判断是否是字母,汉字test = "as2df"v = test.isalpha()print(v) 输入false #当前输入是否是数字test = "二" # 1,②v1 = test.isdecimal()v2 = test.isdigit()v3 = test.isnumeric()print(v1,v2,v3) 输出False False True # 是否存在不可显示的字符# \t 制表符# \n 换行test = "oiuas\tdfkj"v = test.isprintable()print(v)输出false # 判断是否全部是空格test = " "v = test.isspace()print(v) 输出true # 判断是否是标题test = "Return True if all cased characters in S are uppercase and there is"v1 = test.istitle()print(v1) #输出falsev2 = test.title() print(v2) #将上面句子中所有首字母大写,输出结果:Return True If All Cased Characters In S Are Uppercase And There Is v3 = v2.istitle()print(v3) #输出true # ***** 将字符串中的每一个元素按照指定分隔符进行拼接test = "你是风儿我是沙"# t = ' 'v = "_".join(test)print(v) 输出:你_是_风_儿_我_是_沙 # 18 判断是否全部是大小写 和 转换为大小写test = "Alex"v1 = test.islower()v2 = test.lower()print(v1, v2) 输出:false alex v1 = test.isupper()v2 = test.upper()print(v1,v2) 输出:false AEX # 去除左右空白test="aa"v = test.lstrip()v1 = test.rstrip()v2 = test.strip()print(v,v1,v2) # 分割为三部分test = "testasdsddfg"v = test.partition('s')print(v) 输出:('te', 's', 'tasdsddfg') 从左往右分割v = test.rpartition('s')print(v) 输出:('testasd', 's', 'ddfg') 从右往左按s进行分割 # 22***** 分割为指定个数 test = "testasdsddfg"v = test.split('s',2)print(v) 输出:['te', 'ta', 'dsddfg']  

转载于:https://www.cnblogs.com/yxks/p/10810413.html

相关资源:数据结构—成绩单生成器
最新回复(0)