python 常见字符串处理方法

什么是字符串

字符串是以单引号或双引号括起来的任意文本都是字符串
#创建字符串
abc=“这里面的是字符串”

#字符串连接
链接字符串用符号“+”

#字符串不可变

常见的 python 字符串处理方法

#输出重复字符串
str1 = "good"
str2 = str1*3
print(str2)

#访问字符串某个字符及截取字符串
str3 = 'sunck is a good man'
print(str3[1:6])#从字符串第二个开始,第6个结束
print(str3[:6])#从第一个开始到第6个结束
print(str3[6:])#从第6个开始直到结束
print("good1" in str3) #判断good 在没在 str3中包含,如果在输出 True,如果不在输出False

#格式化输出
#占位符
# %d:整数占位符
# %s:字符串占位符
# %f:浮点数占位符(可用%.f  精确小数点后保留多少位。)

#以下为转义字符\
#\n:换行符
#\'转义单引号及双引号
#'''
  # 1
  # 2
  # 3
# '''  多行换行(省略#号)
#\t;制表符,默认四个空格
# r :如果字符串之中要转义很多的字符串,那么python 允许r不转义
str4 = 10
str5 = "sunck is a nice man"
str6 = 10.567
print("str4 = %d , str5 = %s , str6 = %.2f," % (str4,str5,str6))
print("str4 = %d \nstr5 = %s \nstr6 = %.2f" % (str4,str5,str6))
print("sunck is a \"nice\" man")
print('''
str4 = %d
str5 = %s
str6 = %.2f
'''
% (str4,str5,str6))
print("str4 = %d \tstr5 = %s \tstr6 = %.2f" % (str4,str5,str6))
print(r"C:\Users\ASUS\Desktop\python\.idea\inspectionProfiles")

#处理字符串的方法

# eval():将字符串当成邮箱表达式来求值,并返回计算结果
num=eval("123456")
print(num)
print(type(num))
print(eval("100+100"))

#len():返回字符串的长度
num1 = len ("123456")
print(num1)

#变量.lower():转换变量字符串中大写为小写
num2 = "Sunck Is a nice man"
print(num2.lower())

#变量.upper():转换变量字符串中小写为大写
num3 = "Sunck Is a nice man"
print(num3.upper())

#变量.swapcase():转换变量字符串中小写为大写,大写字母为小写字母
num3 = "Sunck Is a nice man"
print(num3.swapcase())

#变量.capitalize():转换变量首字母为大写,其他为小写字母
num3 = "Sunck Is a nice man"
print(num3.capitalize())

#变量.title():每个首字母为大写
num3 = "Sunck Is a nice man"
print(num3.title())

#变量.center(width,"fillchar"):返回指定宽度的字符串居中,fillchar为填充字符如果不写,默认空格
num3 = "Sunck Is a nice man"
print(num3.center(40,"_"))

#变量.ljust(width,"fillchar"):返回指定宽度的字符串左对齐,fillchar为填充字符如果不写,默认空格
num3 = "Sunck Is a nice man"
print(num3.ljust(40))

#变量.rjust(width,"fillchar"):返回指定宽度的字符串右对齐,fillchar为填充字符如果不r写,默认空格
num3 = "Sunck Is a nice man"
print(num3.rjust(40))

#变量.zfill(width):返回指定宽度的字符串右对齐 前面补0
num3 = "Sunck Is a nice man"
print(num3.zfill(40))

#变量.count('字符串'):返回字符串中字符出现的次数
#变量.count('字符串',15,len(变量)):在字符传中找某个字符,15为从第15个开始
num3 = "Sunck Is a a nice man"
print(num3.count("nice",13,len(num3)))

#变量.find('字符串'):检测字符串是否包含在变量字符串之中,得到的值为nice开始位置数,没有返回-1
#变量.find('字符串',15,len(变量)):从字符串中找某个字符,15为从第15个开始,得到的值为从指定位置nice开始位置数
num3 = "Sunck Is a a nice nice man"
print(num3.find("nice"))
print(num3.find("good"))
print(num3.find("nice",13,len(num3)))

#变量.rfind('字符串'):检测字符串是否包含在变量字符串之中,得到的值为nice开始位置数,没有返回-1  从右向左打印
#变量.rfind('字符串',15,len(变量)):从字符串中找某个字符,15为从第15个开始,得到的值为从指定位置nice开始位置数,从右向左打印
num3 = "Sunck Is a a nice nice man"
print(num3.rfind("nice"))
print(num3.rfind("good"))
print(num3.rfind("nice",0,17))

#变量.index("字符串"):检测字符串是否包含在变量字符串之中,如果不在程序报错
num3 = "Sunck Is a nice nice man"
print(num3.index("nice"))

#变量.rindex("字符串"):检测字符串是否包含在变量字符串之中,如果不在程序报错
num3 = "Sunck Is a nice nice man"
print(num3.rindex("nice"))

#变量.lstrip():截掉左侧字符串,默认为空格,也可指定字符
num3 = "_________Sunck Is a nice nice man********************"
print(num3.lstrip("_"))

#变量.rstrip():截掉右侧字符串,默认为空格,也可指定字符
num3 = "_________Sunck Is a nice nice man****************"
print(num3.rstrip("*"))

#变量.strip():截掉两边字符串,默认为空格,也可指定字符
num3 = "_________Sunck Is a nice nice man****************"
print(num3.strip("_,*"))

#ascll 转换
num3 = "a"
print(ord(num3))#将字母“a“转化为ascll码表

num3 = 97

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值