目录
- find()
- index()
- count()
- startswith()
- endswith()
- isalnum()
- isalpha()
- isdigit()
- replace()
- split()
- splitlines()
- strip()
- title()
- lower()
- upper()
- join()
- 易错类型转换
- 切片
find()
查询指定元素的下标,如果查询的元素不存在,返回-1
str1 = "是微风,是晚霞,是心动不止和无可代替。"
print(str1.find("是")) # 0
print(str1.find("你")) # -1
print(str1.find("是", 5, 10)) # 8 范围内进行查询
print(str1.rfind("是")) # 8 从右往左查询
#执行结果:
0
-1
8
8index()
查询指定元素的下标,如果查询的元素不存在,则报错:
str1 = "是微风,是晚霞,是心动不止和无可代替。"
print(str1.index("是")) # 0
print(str1.index("你")) # ValueError: substring not found
print(str1.index("是", 5, 10)) # 8 范围内进行查询
print(str1.rindex("是")) # 8 从右往左查询
#执行结果:
0
ValueError: substring not found
8
8count()
统计元素在字符串中出现的次数
str1 = "是微风,是晚霞,是心动不止和无可代替。"
print(str1.count("是")) # 3
#执行结果:
3startswith()
判断字符串以…开头
str1 = "是微风,是晚霞,是心动不止和无可代替。"
print(str1.startswith("是")) # True
#执行结果:
Trueendswith()
判断字符串以…结尾
str1 = "是微风,是晚霞,是心动不止和无可代替。"
print(str1.endswith("。")) # True
#执行结果:
Trueisalnum()
判断字符串里是否都是数字、英文、中文
str1 = "是微风,是晚霞,是心动不止和无可代替。"
text = "是微风 是晚霞 是心动不止和无可代替"
str2 = "abc123"
print(str1.isalnum()) # False
print(text.isalnum()) # True
print(str2.isalnum()) # True
#执行结果:
False
True
Trueisalpha()
判断字符串里是否都是英文
str3 = "abc"
str2 = "abc123"
print(str3.isalpha()) # True
print(str2.isalpha()) # False
#执行结果:
True
Falseisdigit()
判断字符串里是否都是数字
str4 = "123"
str2 = "abc123"
print(str4.isdigit()) # True
print(str2.isdigit()) # False
#执行结果:
True
Falsereplace()
replace(替换前,替换后),将括号内的字符串进行替换
如果被替换的元素不存在,则返回原字符串
str5 = "hello world"
print(str5.replace("world", "python")) # hello python
#执行结果:
hello python
str1 = "hello world"
print(str1.replace("aaa", "python")) # 如果被替换的元素在字符串中不存在,则返回原字符串
#执行结果:
hello worldsplit()
将字符串以指定的元素切割,默认以空格切割。
str5 = "hello world"
print(str5.split()) # ['hello', 'world']
str6 = "abcdabcabc"
print(str6.split("b")) # ['a', 'cda', 'ca', 'c']
#执行结果:
['hello', 'world']
['a', 'cda', 'ca', 'c']splitlines()
将字符串按换行符进行切割
str7 = "满天都是星星\n好像一场冻结了的大雨"
print(str7.splitlines()) # ['满天都是星星', '好像一场冻结了的大雨']
#执行结果:
['满天都是星星', '好像一场冻结了的大雨']strip()
去除字符串两边指定的元素,默认去除空格
str8 = " 可以再浪漫些,日落、晚风和鲜花。 "
print(str8.strip()) # 去除两边的空格
print(str8.lstrip()) # 只去除左边空格
print(str8.rstrip()) # 只去除右边空格
#执行结果:
可以再浪漫些,日落、晚风和鲜花。
可以再浪漫些,日落、晚风和鲜花。
可以再浪漫些,日落、晚风和鲜花。
str1 = "ab1234"
str2 = "ab1234ab"
print(str1.strip("ab")) # 去除字符串两边指定的元素 "ab"
print(str2.strip("ab"))
#执行结果:
1234
1234title()
每个单词首字母大写
str5 = "hello world"
print(str5.title()) # Hello World
#执行结果:
Hello Worldlower()
将字符串变成小写
str9 = "HELLO WORLD"
print(str9.lower()) # hello world
#执行结果:
hello worldupper()
将字符串变成大写
str10 = "hello world"
print(str10.upper()) # HELLO WORLD
#执行结果:
HELLO WORLDjoin()
将括号内的序列按照前面双引号中的字符进行拼接
list1 = ['p', 'y', 't', 'h', 'o', 'n']
str1 = "".join(list1)
print(str1)
#执行结果:
python易错类型转换
当字符串中是浮点型数值时,转换为整型会发生错误。
str11 = "5.5"
int(str11) # ValueError: invalid literal for int() with base 10: '5.5'
#执行结果:
ValueError: invalid literal for int() with base 10: '5.5'切片
字符串名[起始值:结束值:步长]
起始值不写,默认从第0个索引开始
结束值不写,默认是整个字符串
步长不写,默认是1
取值范围:左闭右开
str2 = "自由散漫的凉风,能治愈乱糟糟的坏心情。"
print(str2[:7]) # 自由散漫的凉风
print(str2[::-1]) # 。情心坏的糟糟乱愈治能,风凉的漫散由自
print(str2[-1:-10:-1]) # 。情心坏的糟糟乱愈
#执行结果:
自由散漫的凉风
。情心坏的糟糟乱愈治能,风凉的漫散由自
。情心坏的糟糟乱愈
















