# Python字符串 # 大多数的语言定义字符串是双引号,Python既可以双引号,也可以单引号。但使用也有区别 # 单双引号的使用 My_name="bai-boy" Demo="我的名字是bai-boy" Demo01='我的名字是"bai-boy"' # 要想输出“” 外面再加个‘’ # 常用字符串的方法: # 1.统计字符串长度:len() print("My_name字符串个数........") print(len(My_name)) # 2.统计子字符串出现的次数:count() print("My_name字符串中b子字符个数........") print(My_name.count("b")) # 3.统计子字符串出现的位置index print("My_name字符串中b子字符位置........") print(My_name.index("a")) # 4.判断是否全是空白字符 isspace() 是返回true 否返回false print("判断是否全是空白字符........") empty_string=" " print(empty_string.isspace()) # 注意 转移字符也属于空白字符 empty_string1=" \r\r\n" print(empty_string1.isspace()) # 5.判断是否为数字三种方法:都不能判断小数 # (1).isdecimal:只能判断数字 # (2)isdigit:能判断数字或者(1)或者unicode字符编码 # (3)isnumeric:阿拉伯数字及中文数字 num_demo01="3" print(num_demo01.isdecimal()) # true print(num_demo01.isdigit()) # true print(num_demo01.isnumeric()) # true num_demo02="3.1" print(num_demo02.isdecimal()) # false print(num_demo02.isdigit()) # false print(num_demo02.isnumeric()) # fasle num_demo03="一百" print(num_demo03.isdecimal()) # false print(num_demo03.isdigit()) # false print(num_demo03.isnumeric()) # True StringDemo="my name is bai-boy" print(StringDemo) # 6.判断是否为指定字符串开始 print(StringDemo.startswith("my")) # 7.判断是否为指定字符串结束 print(StringDemo.endswith("boy")) # 8.查找指定字符串 返回字符串的启始下标 print(StringDemo.find("name")) # 9.替换指定字符串 并不会更改原有的字符串 而是返回一个新的字符串 print(StringDemo.replace("bai-boy","xiaobai")) # 字符串切片:[开始下标:结束下标:步长]
Python基础学习----字符串的常用方法
猜你喜欢
转载自www.cnblogs.com/bai-boy/p/10335769.html
今日推荐
周排行