# 字符串 -------笔记------ # 1、字符串的特性:不可修改,有序的 # 2、字符串的定义: # 双引号和单引号都可以定义 # 三双引号和三单引号都可以定义 # 字符串里面有空格也算是字符串中的一部分 # 如: test='hello python' test="hello python" test='''hello python''' test="""hello python""" #3、字符串的访问: # 字符串通过索引进行访问 # 索引是从0开始的,正序取索引 # 索引从-1开始的,倒序取索引 # 语法:print(字符串[索引值]) test_str='hello python' #正序取索引为2的字符 print(test_str[2]) #倒序取索引为2的字符 print(test_str[-10]) #4、字符串的切片 # 4.1语法:字符串[起始索引位置:结束索引位置:步长] # 步长:指间隔几个索引取值 # 4.2切片取值注意点: # 左闭右开,取头不取尾,默认步长为1 # 一个空格占一个索引位置 # 不写起始位置默认从0开始 # 不写结束位置默认为字符串最大长度 # 步长前面的符号为+号:正序切片,-号:倒序切片 # 正序切片全部写正数,倒序切片全部写负数 test_str_01='hello python' result=test_str_01[1:3] print(result) print(test_str_01[1:3]) #取索引值0,1,2三个索引值,不取包含3索引位置的值 print(test_str_01[1:6:2])#取步长为2,取1~6位置的索引值,不取6,正序取值 print(test_str_01[-1:-6:-2])#取步长为2,取-1~-6位置的索引值,不取6,倒序取值 #5、字符串运算: # 5.1拼接:+ test1='hello' test2="python" print("输出test1加test2的值:",test1+test2) # 5.2重复输出:* test1='hello' print("输出test1值两边:",test1*2) #6、字符串的转义:\n、r、\\ #如:\n:进行字符换行 test2='hello\npython' print(test2) #r:取消转义 #\\:取消转义 test3=r'hello\npython' test4='hello\\npython' print(test4) #7、字符串常用方法: # 7.1单词大小相关:语法:str.方法() # 7.1.1str.upper()字符串所有单词大写 # 7.1.2str.lower()字符串所有单词小写 # 7.1.3字符串单词首字母大写:str.capitalize() # 7.1.4字符串每个单词首字母大写:str.title() # 7.1.5字符串大小写互换:str.swapcase() # 7.2字符串统计相关:语法:str.方法() # 7.2.1统计字符串在字符出现的次数:str.count(sub,start=None,end=None) # sub:需要统计的字符 # start:统计的索引开始位置,默认是None # end:统计的索引结束位置 # 7.2.2查找字符串在字符出现的索引位置:str.find(sub,start=None,end=None) # sub:需要统计的字符 # start:统计的索引开始位置,默认是None # end:统计的索引结束位置 # 返回值:在字符串中找到第一个匹配的索引位置(未查到字符串中的字符返回-1) # 7.2.3查找字符串在字符出现的索引位置:str.rfind(sub,start=None,end=None) # sub:需要统计的字符 # start:统计的索引开始位置,默认是None # end:统计的索引结束位置 # 返回值:在字符串中找到最后匹配的索引位置(未查到字符串中的字符返回-1) # 如: #将字符串转换为大写 test5='hello python' print(test5.upper()) #将字符串转换为小写 test5='hello python' result_01=test5.lower() print(result_01) print(type(test5))#查看字符串类型 #将单词首字母大写 test5='hello python' result_01=test5.capitalize() print(result_01) #将单词所有首字母大写 test5='hello python' result_01=test5.title() print(result_01) #将字符串中大小写互换 test5='hello PYTHON' result_01=test5.swapcase() print(result_01) #统计字符串中字符出现的次数,索引从0~3 test5='hello PYTHON' result_01=test5.count('h',0,3) print(result_01) #查找字符串中字符出现在索引的第一个位置 test5='hello python' result_01=test5.find('h') print(result_01) #查找字符串中字符未出现在字符串中返回:-1 test5='hello python' result_01=test5.find('m') print(result_01) #查找字符串中字符出现在索引的最后位置 test5='hello python' result_01=test5.rfind('h') print(result_01)
---------------字符串常用方法----- 一、判断相关 1.判断字符串是否都大写: 语法:str.isupper() 是返回True,否则返回Faslse 2.判断字符串是否都小写: 语法:str.islower() 是返回True,否则返回Faslse 3.判断字符串是否为数字、字母 语法:str.isalnum() 是返回True,否则返回False 4.判断字符串是否为汉字: 语法:str.isalpha() 是返回True,否则返回False 5.判断字符串是否都为数字: 语法:str.isdigit() 是返回True,否则返回False 6.判断字符串是否已某个字符开头: 语法:str.startswith() 是返回True,否则返回False 7.判断字符串第一个字母是否大写: 语法:str.istitle() 是返回True,否则返回False 8.判断字符串中间是否为空: 语法:str.isspace() 是返回True,否则返回False ''' # 实例1: str='hello Word' print("判断是否为大写:",str.isupper()) # 实例2: str='hello Word' print("判断是否为小写:",str.islower()) # 实例3: str='hello Word' print("判断字符串是否为数字、字母:",str.isalnum()) # 实例4: str='hello Word' print("判断字符串是否为汉字:",str.isalpha()) # 实例5:场景:数据库 查询做断言使用 str='hello Word' print("判断字符串是否为数字:",str.isdigit()) # 实例6: str='hello Word' print("判断字符串是否已某个字符开头:",str.startswith('e')) # 实例7: str='hello Word' print("判断字符串是否首字母大写:",str.istitle()) # 实例6: str='hello Word' print("判断字符串是否为空:",str.isspace()) ''' 二、字符串拆分和字符串替换 2.1:使用场景 替换:自动化测试时参数替换,请求地址得参数替换 拆分:按规则进行字符串拆分 ---面试需要 2.2:替换:replace 语法:str.replace('old','new','count') old:需要替换的字符 new:替换后的字符 count: 替换得个数,不写得时候默认替换所有 ''' #实例1:替换所有 str_01='hello python' print("替换字符串中的h为e:",str_01.replace('h','e')) #实例2:指定替换次数1 str_01='hello python' print("替换字符串中的h为e替换一个:",str_01.replace('h','e',1)) ''' 2.3:替换:rsplit 语法:str.split('sep','maxsplit') sep:指定按哪个字符进行拆分,不写默认按空格进行拆分 maxsplit:拆分几次,默认(-1)拆分全部不做限制 *面试需要 注意:拆分后会丢弃拆分字符 ''' #实例1:按默认空格进行拆分: str_01='hello python' print("按照默认空格进行拆分:",str_01.split()) #实例2:按某个字符进行拆分: str_01='hello python' print("按照字符o进行拆分:",str_01.split('o')) #实例2:按某个字符进行拆分几次: str_01='hello python' print("按照字符o进行拆分2次:",str_01.split('o',1)) ''' 2.3.1字符串拆分 场景:读取文件,回车符,换行符 语法:str.splitlines() *按照换行符进行拆分 keepends:默认不保留拆分字符,keepends=True''' #实例1:按照换行符进行拆分 str_01='hello\npython' print("按照字符串换行符进行拆分:",str_01.splitlines()) #实例2:按照回车符进行拆分 str_01='hello\rpython' print("按照字符串回车符进行拆分:",str_01.splitlines()) #实例2:保留拆分字符 str_01='hello\rpython' print("按照字符串回车符进行拆分保留拆分字符:",str_01.splitlines(keepends=True)) ''' 2.4字符串连接符:join 场景:计算规则 *面试需要 语法:str.join('需要连接得字符串')''' str_01='hellopython' str_02=' '.join(str_01) #将字符串空格连接 print(' '.join(str_01)) print(str_02.replace(' ','')) ''' 2.5字符串:strip:删除字符前后得空格或换行符,中间不会被删除 语法:str.strip() *不传参默认去掉前后得空格和换行符,传参默认去掉前后最后一位 ''' #实例1:不传参默认去掉前后有空格和换行符 str_01='\nhello python ' print(str_01.strip()) #实例1:传参去掉前后字符,切记只去掉前后一位 str_01='hello python' print(str_01.strip('n''h')) ''' 三、字符串格式化 3.1:%s字符串得占位符 注意:如果不是str类型会进行强制类型转换,python会强制将其他类型转换为str类型 ''' #实例:%s强制将int类型转换为str类型 name='刘航' age=27 test_01='my name is %s age is %s' %(name,age) print(test_01) ''' 3.2:%d数值类型得占位符 注意:如果传入float类型,会去掉小数点部分,保留整数,因为发生了字符串类型强制转换,将小数转换为int类型 数值类型只适用于int类型和float类型 ''' #实例:将float类型转换为int类型, name=1111.111 age=27 test_01='my name is %d age is %d' %(name,age) print(test_01) ''' 3.3:%f,精度处理,默认保留小数位后6位, %.Xf(X为保留几位小数) 场景:在计算数据,对账,数据库自行处理SQL语句处理 ''' #实例1:默认保留6位小数 phone=1000 price='phone is %f' %(phone) print(price) #实例2:默认保留2位小数 phone=1000 price='phone is %.2f' %(phone) print(price) ''' 3.4字符串format格式化 ''' #实例1:按顺序取值:format括号中字符串得顺序 name='刘航' age=27 test_01='my name is {} age is {}'.format(name,name) print(test_01) #实例2:按索引取值:format括号中字符串得索引 name='刘航' age=27 test_01='my name is {0} age is {1}'.format(name,age) print(test_01) #实例3:按关键字取值:format括号中字符串得关键字 name='刘航' age=27 test_01='my name is {liuhang} age is {age}'.format(liuhang=name,age=age) print(test_01) #3.5:f表达式 name='刘航' age=27 test_01=f'my name is {name} age is {age}' print(test_01)