(接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

未完……

标签: 达内, 软件测试, Python

分类: 个人日志, 软件测试学习, Python, 学点技术

添加新评论