python之字符串

用引号包括起来的集合称之为字符串,其引号可以是单引号,双引号,三引号(单/双),三引号可以将复杂的字符串进行复制,允许一个字符串跨多行,字符串可包含制表符、换行符、以及其它特殊符号。

str1=’hello’
str2=”ligbee”
str2=”’the blog”’


字符串运算符 p=’python’

运算符解释使用
+字符串连接p+p = pythonpython
*重复输出字符串p*2 = pythonpython
[]通过索引获取字符p[0]= p
[:]截取字符串一部分p[1:] = ython
in成员运算符‘p’ in p = true
not in成员运算符‘a’ not in p = false
r or R原始字符串,转义失效r’python\n’ = python\n

字符串格式化

python 格式化与 C 一样使用占位符%

'the %s was created in %d' % ('python',1989)
'the python was created in 1989'

字符串函数 p=’the python was created in 1989’
start=0 end=len(p)
查找拥有方法,p. Tab
查找使用方法,help(p.find)

函数解释使用
find返回对应字符串位置,查找不到返回-1p.find(‘python’,start,end)
rfind从右边开始查找,返回对应字符串位置,查找不到返回-1p.rfind(‘python’,start,end)
index返回对应字符串位置,查找不到报错p.index(‘python’,start,end)
rindex从右边开始查找,返回对应字符串位置,查找不到报错p.rindex(‘python’,start,end)
count返回对应字符串个数p.count(‘python’,start,end)
decode以encoding指定的编码解码字符串p.decode(encoding=’UTF-8’,errors=’strict’)
encode以encoding指定的编码编码字符串p.encode(encoding=’UTF-8’,errors=’strict’)
replace将str1替换为str2p.replace(str1,str2,num(替换次数))
split以指定分隔符p.split(” “,num)
capitalize首字符大写p.capitalize
center返回左右被空格填充的长度为num的字符串,居中p.center(num)
ljust左对齐p.ljust(num)
rjust右对齐p.rjust(num)
endswith检查字符串是否以str结尾p.endswith(str,start,end)
startswith检查字符串是否以str开头p.startswith(str,start,end)
expandtabs将tab转换为空格,默认空格数为8p.expandtabs(8)
isalnum至少有一个数字并且所有字符都是数字或字母p.isalnum()
isalpha至少有一个字符并且所有字符都是字母(不包括空格)p.isalpha()
isdecimal只包含十进制
isdigit只包含数字
islower判断都是小写
isupper判断都是大写
isspace只有空格
lower转换为小写
upper转换为大写
join连接字符串strp.join(str)
lstrip截掉左边空格p.lstrip()
rstrip截掉右边空格p.rstrip()
partition以str分割线,分成三部分,返回元组p.partition(str)
rpartition以str分割线,从右分成三部分,返回元组p.rpartition(str)
zfill右对齐,0填充p.zfill(num)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值