python 字符串

字符串 

字符串时一个有序的字符集合,用于存储和表示基本的文字信息,’ ‘或’’ ‘’或’’’ ‘’’中间包含的内容称之为字符串

>>> s = 'Hello,Eva!How are you?'

特性:

1.按照从左到右的顺序定义字符集合,下标从0开始顺序访问,有序

3.可以进行切片操作

4.不可变,字符串是不可变的,不能像列表一样修改其中某个元素,所有对字符串的修改操作其实都是相当于生成了一份新数据。

5.字符串的单引号和双引号都无法取消特殊字符的含义,如果想让引号内所有字符均取消特殊意义,在引号前面加r,如name=r’l\thf’

>>> name = r"Ni\tce\n"
>>> name
'Ni\\tce\\n'
>>> print(name)
Ni\tce\n

1 字符串常用操作

1.1查找

find   #定位索引位置

>>> a
'nis chaopeng Nice'
>>> a.find("i")   #定位索引位置
1

count  #统计字符串中对的字符

>>> a
'nis chaopeng Nice'
>>> a.count("c")  #统计字符串中的c
2
>>> a.count("c",2,8)  #统计字符串中第2到第8列中的c
1

index  #查看元素是否在字符串中位置

>>> a
'nis chaopeng Nice'
>>> a 
'nis chaopeng Nice'
>>> "ao" in a
True
>>> a.index("ao")
6
>>> a.index("o")
7
>>> a.index("a")
6

1.2 改

replace  #修改

>>> a = "My score is 680, no very good."
>>> a.replace("680","700")
'My score is 700, no very good.'
>>> a = "My score is 680, no very good 680 ."
>>> a.replace("680","700")
'My score is 700, no very good 700 .'
>>> a.replace("680","700",1)
'My score is 700, no very good 680 .'

upper

>>> a 
'nis chaopeng Nice'
>>> a.upper()
'NIS CHAOPENG NICE'

lower   #全小写

>>> "NICE".lower()
'nice'

capitalize  #开头字母大写其他全小写

>>> a
'nis chaopeng Nice'
>>> a.capitalize()
'Nis chaopeng nice'

casefold   #字符串全小写

>>> a
'nis chaopeng Nice'
>>> a.casefold()
'nis chaopeng nice'

swapcase

>>> b = "Nice"
>>> b.swapcase()
'nICE'

strip  #取消空格

>>> c = "\t nice \t"
>>> c
'\t nice \t'
>>> c.strip()
'nice'
>>> c.lstrip()
'nice \t'
>>> c.rstrip()
'\t nice'

split  #字符串转换成列表 从左开始分

>>> m1
'nis,chaopeng,chaoyang,xiaodong,yongkang'
>>> m1.split()
['nis,chaopeng,chaoyang,xiaodong,yongkang']
>>> m1.split("|")
['nis,chaopeng,chaoyang,xiaodong,yongkang']
>>> m1 += " hehe | dd"
>>> m1
'nis,chaopeng,chaoyang,xiaodong,yongkang hehe | dd'
>>> m1.split("|")
['nis,chaopeng,chaoyang,xiaodong,yongkang hehe ', ' dd']
>>> m1.split(" ")
['nis,chaopeng,chaoyang,xiaodong,yongkang', 'hehe', '|', 'dd']
>>> m1.split("|,")
['nis,chaopeng,chaoyang,xiaodong,yongkang hehe | dd']
>>> m1.split(",",1)
['nis', 'chaopeng,chaoyang,xiaodong,yongkang hehe | dd']

rsplit #字符串转换成列表   从右开始分

>>> m1
'nis,chaopeng,chaoyang,xiaodong,yongkang hehe | dd'
>>> m1.rsplit("1")
['nis,chaopeng,chaoyang,xiaodong,yongkang hehe | dd']
>>> 
>>> m1.rsplit("|")
['nis,chaopeng,chaoyang,xiaodong,yongkang hehe ', ' dd']
>>> m1.rsplit("|,")
['nis,chaopeng,chaoyang,xiaodong,yongkang hehe | dd']
>>> m1.rsplit(",",1)
['nis,chaopeng,chaoyang,xiaodong', 'yongkang hehe | dd']

1.3 格式化

format & %s

>>> s = "Welcome {0} to Apeland,you are No.{1} user."
>>> s.format("nis",1)
'Welcome nis to Apeland,you are No.1 user.'
>>> s = "Welcome {0} to Apeland,you are No.{1} user.{0}."
>>> s.format("nis",4)
'Welcome nis to Apeland,you are No.4 user.nis.'
>>> s = "Welcome {name} to Apeland,you are No.{user_name} user."
>>> s.format(name='nis',user_name='1')
'Welcome nis to Apeland,you are No.1 user.'
>>> s =  "My name is %s , i am %s years old" %("alex",23)
>>> s
'My name is alex , i am 23 years old'

ljust   #右拼接

>>> a
'nice_yongkang_chaoyang_xiaodong'
>>> a.ljust(50,"*")
'nice_yongkang_chaoyang_xiaodong*******************'

rjust   #左拼接

>>> a
'nice_yongkang_chaoyang_xiaodong'
>>> a.rjust(50,"*")
'*******************nice_yongkang_chaoyang_xiaodong'

join  #拼接

>>> m = ["nice","yongkang","chaoyang","xiaodong"]
>>> "".join(m)
'niceyongkangchaoyangxiaodong'
>>> " ".join(m)
'nice yongkang chaoyang xiaodong'
>>> "_".join(m)
'nice_yongkang_chaoyang_xiaodong'
>>> m
['nis', 'chaopeng', 'chaoyang', 'xiaodong', 'yongkang']
>>> ",".join(m)
'nis,chaopeng,chaoyang,xiaodong,yongkang'
>>> m1 = ",".join(m)
>>> m1
'nis,chaopeng,chaoyang,xiaodong,yongkang'

1.4 判断

isdigit  #判断是否为整数

>>> "tuew".isdigit()
False
>>> "3".isdigit()
True
>>> "3.3".isdigit()
False

startswith  #判断开头

>>> a
'nis chaopeng Nice'
>>> a.startswith("n")
True
>>> a.startswith("m")
False

endswith

>>> a
'nis chaopeng Nice'
>>> a.endswith("e")
True
>>> a.endswith("E")
False

islower   #判断是否为小写

>>> "dafj".islower()
True
>>> "dRfj".islower()
False

isspace #判断是否为空格

>>> ''.isspace()
False
>>> ' '.isspace()
True

isupper #判断是否都为大写

>>> "TUER".isupper()
True
>>> "TUERe".isupper()
False

2.补充

center #补充长度

>>> a
'nis chaopeng Nice'
>>> a.center(36,"-")
'---------nis chaopeng Nice----------'

zfill #补充长度用0补充

>>> "nisc".zfill(34)
'000000000000000000000000000000nisc'

expandtabs  #把\t转换成tabsize个空格

'ajlkfalajdkla   adfa'
>>> a 
'ajlkfalajdkla\tadfa'
>>> a.expandtabs()
'ajlkfalajdkla   adfa'
>>> a.expandtabs(2)
'ajlkfalajdkla adfa'

isalnum #至少一个字符,且都是字母或数字才返回True

>>> a
'ajlkfalajdklaadfa'
>>> b
'ahds$'
>>> a.isalnum()
True
>>> b.isalnum()
False

isalpha #至少是一个字符且都是字母才返回true

>>> a
'ajlkfalajdklaadfa'
>>> b
'ahds$'
>>> c
'akdhfa3'
>>> a.isalpha()
True
>>> b.isalpha()
False
>>> c.isalpha()
False

join #

>>> li = ['alex', 'eric', 'rain']
>>> print(" ".join(li))
alex eric rain
>>> print("_".join(li))
alex_eric_rain
>>> a = "hello good boy doiido"
>>> print(":".join(a))
h:e:l:l:o: :g:o:o:d: :b:o:y: :d:o:i:i:d:o
>>> b= ('hello','good','boy','doiido')
SyntaxError: invalid syntax
>>> print(':'.join(b))
hello:good:boy:doiido
>>> c = {'hello':1,'good':2,'boy':3,'doiido':4}
>>> print(':'.join(c))
hello:good:boy:doiido
>>> 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值