一、大小写相关的
1.验证码 不区分大小写
全部大写:变量名.upper()
全部小写:变量名.lower()
str=“python”
str1=str.upper()
Pycharm 代码中间换行快捷键:ctrl+shift+enter
2.首字母大写:变量名.capitalize()
3.每一个首字母大写:变量名.title()
4.大小写互换:变量名.swapcase()
str=“python Hello”
print(str.swapcase())
二、统计相关的
- count():统计字符在字符串中出现的次数
str.count(str, start,end) - find():查找字符在字符串中的索引(默认只查找第一次找的的字符的索引)
str.find(str, start=0, end=len(string))
str,指定检索的字符串
start,开始索引,默认为0。
end,结束索引,默认为字符串的长度。
代码格式化快捷键:ctrl+alt+L
看源码:ctrl+鼠标单击
三、拆分&替换
1.split(“指定字符拆分”,拆分的次数):字符串的拆分,默认通过空格键无限制(-1)拆分;拆分的过程会将指定的字符丢弃
2.replace(“被替换的字符”,“新字符”,替换次数)
四、字符串的连接
join:通过指定的字符(串)连接字符串
str=“python hello”
new_str=“—”.join(str)
>>p-y-t-h-o-n- -h-e-l-l-o
seq1 = ['hello','good','boy']
print ' '.join(seq1)
>>hello good boy
五.字符串格式化
1.%占位符:(参数化)
%s 字符串 %d int类型 %f float类型
str="i love u more than %d times" %(1000)
print(str)
str="hi,the price is %.3f dollars" %(9.9)
print(str)
2.format{}
a.按顺序取值
str="hi,the price is {}、{}、{} dollars" .format(100,90,80)
print(str)
b.按索引取值
str="hi,the price is {0}、{1}、{1} dollars" .format(100,90)
print(str)
c.按关键字取值
str="hi,the price is {price1}、{price1}、{price2} dollars" .format(price1=100,price2=90)
print(str)
d.调整精度(数据库结果处理)
str="hi,the price is {:.2f} dollars" .format(3.1415926)
print(str)
#百分比格式化
str1="hi,the price is {:.2%} dollars" .format(3.1415926)
print(str1)
3. f表达式
price=100
str=f"hi,the price is {price} dollars"
print(str)