软件测试-DAY37-Python3
(接DAY36)
四、运算符和表达式
字符串格式化(占位符)
f'{}'
{}
中可存放变量名或表达式
示例:
>>> x = 100
>>> y = 2
>>> print(f'{x}+{y}={x+y}')
100+2=102
字符串解包
示例:
>>> str01 = 'ABCD'
>>> w1, w2, w3 ,w4 = str01
>>> print(w1, w2, w3 , w4, sep='-')
A-B-C-D
获取字符串长度
示例:
>>> str = 'Python真好学!'
>>> str_len = len(str)
>>> print(str_len)
10
index()
index()
方法用于检测字符串中是否包含子字符串
示例:
>>> str1 = "今天的天气真不错"
>>> str2 = "天气"
>>> print (str1.index(str2))
>>> print (str1.index(str2, 0, len(str1)))
3
3
count()
count()
方法用于统计字符串里某个字符出现的次数
示例:
>>> str1 = 'qqays'
>>> str2 = "1"
>>> print(str1.count(str2))
2
split()
split()
通过指定分隔符对字符串进行切片
示例:
>>> str1 = 'qqAys/Google/Aliyun/Baidu'
>>> out = str1.split('/')
>>> print(out)
>>> print(type(out))
['qqAys', 'Google', 'Aliyun', 'Baidu']
<class 'list'>
处理URL,保留保留最后一个切片
>>> url = "https://qqays.xyz/usr/uploads/2022/04/1290079820.jpeg" >>> path =url.split("/")[-1] >>> print(path) 1290079820.jpeg
lower()
、upper()
lower()
方法转换字符串中所有大写字符为小写;upper()
方法将字符串中的小写字母转为大写字母。
示例:
>>> str1 = 'qqAys'
>>> print(str1.lower())
>>> print(str1.upper())
qqays
QQAYS
未完……
版权属于:qqAys
本文链接:https://qqays.xyz/archives/softwaretest-day37-python-3.html
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
您可以自由的转载和修改,但请务必注明文章来源并且不可用于商业目的。