本文主要介绍Python的字符串函数
文章目录
前言
提示
提示:以下是本篇文章正文内容,下面案例可供参考
一、第一部分:字符串中函数中的关于大小写相关的
1.capitalize()
2.title()
3.upper()
4.lower()
5istitle()
message='Aq is a handsome girl'
msg=message.capitalize() #将字符串的第一个字符串转换成大写
print(msg)
msg =message.title()# 每个单词的首字母大写
print(msg)
result=msg.istitle() #返回的结果是布尔类型的,True False,判断是否全部单词的首字母大写
print(result)
msg=message.upper() #将字符串全部转换成大写的表示
print(msg)
result=msg.lower() #将大写全部转换小写
print(result)
验证码的编码案例:
#案例:验证码案例
s='QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0987654321'
print(len(s)) #求字符串的长度 len(str),返回值是一个整形的数值
#四个随机数
code=''
import random
# IndexEerror: string index out of range s='abc' print(s[3])
for i in range(4):
ran = random.randit(0,len(s)-1)
code+=s[ran]
print('验证码:'+code)
#提示用户输入验证码
user_input = input('请输入验证码:')
if user_input.lower() == code.lower():
print("验证码输入正确")
else:
print("验证码错误!")
二、查找和替换相关的字符内建函数
1.find
2.rfind
3.lfind
4.index()
5.lindex()
6.rindex()
7.relpace()
代码如下(示例):
s1='index lucy lucky goods'
result= 'R' in s1
print(result)
position =s1.find('R') #返回值是-1则代表没有找到
print(position)
position=s1.find('1')