Python3 字符串内置函数

在Python3中,本身就已经内置有很多的函数:

capitalize()  首字母强制转换为大写

test="chiLdren"
v=test.capitalize() 
print(v)

casefold()  所有字符全部转换为小写

v1=test.casefold() 
print(v1)

 

islower 用来判断是否是全部小写,lower 用来把所有字符转换为小写

test="chenchaoZHEN"
v1=test.islower()
v2=test.lower()
print(v1,v2)

 


运行结果:
False chenchaozhen


swapcase 用来做小写转换为大写,大写转换为小写

test="ChenCHAO"
v=test.swapcase()
print(v)

运行结果
cHENchao


center 居中,不满字节,以自定义符号填充

test="children"
v4=test.center(20,"*")
print(v4)

运行结果:

******children******

 

ljust 靠左,不满字节 以自定义符号填充

test="children"
v=test.ljust(20,"*")
print(v)

 

rjust 靠右,不满字节,以自定义符号填充

test="children"
v=test.rjust(20,"*")
print(v)

 

zfill 默认以0 做填充

test="alex"
v=test.zfill(20)
print(v)

count() 统计的次数

test="aLexalex"
v=test.count('e') 
print(v) 

运行结果:

2

startswith()   判断开头

endswith() 判断结尾

test="alex"
v=test.endswith('x')  #是否以x结尾
v1=test.startswith('x')  #是否以x开头
print(v)
print(v1)

 

运行结果:

True

False

find() 查找

test="alxxexalex"
v=test.find('ex')  #从开始往后找,找到第一个之后获取它的位置,然后结束
print(v)  #-1 代表没找到

运行结果:

4

index功能和find是一样的,但是index找不到就会报错,find不会,建议用find

 

format 相当于字典

test="i am {name},age:{a}" # {}占位符
print(test)
v=test.format(name="children",a=19)  #以children替换test里面的name
print(v)

运行结果:

i am {name},age:{a}
i am children,age:19

 

test1="i am {0},age:{1}"
v1=test1.format("children",19)  #默认按顺序替换
print(v1)

运行结果:

i am {name},age:{a}
i am children,age:19




isalnum 用来判断是否是字母或数字

test="usaf890"
v=test.isalnum()
print(v)

运行结果:

True

 

expandtabs 用于分割字符串,或者做自动补齐
\t 不满足的时候,遇到\t就是以空格填充
\n 自动换行

test="username\temail\tpassword\nchenchaozhen\t112323@qq.com\tadmin1\nxuzhenzhi\t42323@qq.com\tadmin2"
v=test.expandtabs(20)
print(v,len(v))

 


运行结果

username email password
chenchaozhen 112323@qq.com admin1
xuzhenzhi 42323@qq.com admin2 142


isalpha 用来判断是否为纯字母,返回希尔值

test="asdedf22"
v=test.isalpha()
print(v)

 


运行结果:
False

isdigit isdecimal 用来判定输入是否是纯数字

test1="123"
test2="123a"
v1=test1.isdecimal()
v2=test1.isdigit()
v3=test2.isdecimal()
v4=test2.isdigit()
print(v1,v2)
print(v3,v4)

 


运行结果:
True True
False False

isnumeric 用来判断是否是纯数字,但是支持中文

test=""
v=test.isnumeric()
print(v)

 


运行结果
True

isprintable 用来判断是否存在不可显示的字符
\t 制表符
\n 换行符

test="asde\t\nffg"
v=test.isprintable()
print(v)

 

运行结果
False

isspace 用来判断是否全部为空格

test="chen chao zhen "
v=test.isspace()
test1=" "
v1=test1.isspace()
print(v,v1)

 


运行结果:
False True

istitle 用来判断单词首字母是否为大写
title用来把单词首字母转化为大写

test="Return True if S is a titlecased string and there is at least one character in S,"
v=test.title()
print(v)
v1=v.istitle()
print(v1)

 


运行结果:
Return True If S Is A Titlecased String And There Is At Least One Character In S,
True

isupper 用来判断是否是全部大写

test="CHENN CHAON ZHEN"
test1="chen chao ZHEN"
v1=test.isupper()
v2=test1.isupper()
print(v1,v2)

 


运行结果:
True False


join 用于分割字符串,以特定的符号

test="chenchaozhen"
t='*'  #以*分割字符
v=t.join(test)
print(v)

 


运行结果
c*h*e*n*c*h*a*o*z*h*e*n

lstrip 移除左边空白 \t \n
rstrip 移除右边空白 \t \n
strip 移除两边空白 \t \n
也可以指定移除特定内容 ,优先最多匹配

test="alexal"
v=test.lstrip('al')  #从左边开始移除al
v1=test.rstrip('al') #从右边开始移除
v2=test.strip('al') #从两边开始移除
print(v,v1,v2)

运行结果:

exal alex ex

maketrans 做对应关系
translate 做替换

test="chen56"
m=str.maketrans("chen","1234")
new_test=test.translate(m)
print(new_test)

 


运行结果:
123456

partition 以指定字符分割字符串,从左开始,匹配到了就结束,不会继续往下匹配
rpartition 以指定字符分割字符串,从右开始,匹配到了就结束,不会继续往下匹配
split 以指定字符分割,会一直匹配到结尾

test="testschilszhen"
v=test.partition ('chi')
v1=test.rpartition('s') #分隔符不会被剔除
v2=test.split('s',2)  #分割2次,分隔符会被剔除
v3=test.rsplit('s',2)
print(v)
print(v1)
print(v2)
print(v3)

 


运行结果:
('tests', 'chi', 'lszhen')
('testschil', 's', 'zhen')
['te', 't', 'chilszhen']
['test', 'chil', 'zhen']

splitlines 只根据换行符来分割,True代表不分割,False分割

test="chen\nchao\nzhen"
v=test.splitlines(True)
v1=test.splitlines(False)
print(v)
print(v1)

 


运行结果:
['chen\n', 'chao\n', 'zhen']
['chen', 'chao', 'zhen']


replace 用指定内容去替换指定数据

test="chenchaozhenchen92"
v=test.replace('chen','children',2) #2代表替换次数
print(v)

运行结果:
childrenchaozhenchildren92

7个基本常用的函数
join
split
find
strip
upper
lower
replace

特别函数
通过索引下标获取字符串的某一个字符

test="alex"
v=test[0]
v1=test[1]  #单个取
v2=test[0:2]  #范围取
v3=test[0:-1]  #-1代表最后一个
print(v)
print(v1)
print(v2)
print(v3)

 


运行结果:
a
l
al
ale

len 获取字符串由几个字符组成

test="alex"
v=len(test)
print(v)

 


运行结果:
4

逐个取字符,用for in 函数
name="chenchaozhen"
for num in name:
print(num)
for循环在其他也能使用
索引,切片也能使用
test="chen\tchao\tzhen"
v=test.split('\t')
print(v)
字符串一旦创建就不可修改
一旦修改或者拼接,都会造成重新生成字符串

 

转载于:https://www.cnblogs.com/children92/p/9113815.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值