python3基础:介绍几个函数的用法
1 find()函数:找到返回对应字符的索引 返回-1表示未找到字符串
s = 'hello python apple'
print(s.find('n')) print(s.find('5')) print(s.find('python')) #返回字符串第一个索引值 print(s.find('p',7)) #寻找目标子字符串 寻找开始索引位置
输出结果
11 -1 6 14
2 isdigit()函数:如果只包含数字,就返回true,否则返回false
s = 'hello python apple'
print(s.isdigit()) print('111'.isdigit())
输出结果
False
True
3. replace()函数 指定替换内容以及替换字符串,并且可以指定替换次数 默认全部替换
s = 'hello python apple'
new_s = s.replace('p','k') print(new_s) new_s = s.replace('p','k',1) # 1表示替换的位置 print(new_s)
输出结果
hello kython akkle
hello kython apple
4. split()函数 根据指定字符串经销切割
s = 'hello python apple' print(s.split()) print(s.split(' ',1)) print(s.split('l')) # 切割的字符为空字符串
输出结果:
['hello', 'python', 'apple'] ['hello', 'python apple'] ['he', '', 'o python app', 'e']
5. strip()函数:去掉头和尾指定的字符
e = '@@@hello python apple@' e_1 = e.strip('@') print(e_1)
输出结果
hello python apple
6. upper() 把字符串转换成大写
lower()把字符串转换成小写
s = 'hello python apple' s1="KfSHDfKJdfH"
print(s.upper())
print(s1.lower())
输出结果:
HELLO PYTHON APPLE
kfshdfkjdfh
7. swapcase()函数 大小写互换
s1="KfSHDfKJdfH" print(s1.swapcase())
输出结果:
kFshdFkjDFh
转载请附上原文链接。