Pyhon中的字符串《九》

一、 创建字符串

s1 = 'python'
s2 = "python"

二、字符串的操作

print('python'*3)#重复输出
pythonpythonpython
print('python'[3:])#字符串的切片
hon
print('py' in 'python')#判断是否再这个字符串里
print('aaa' in 'python')
True
False
print('python is a better ')
print('%s is a netter'%'python')
python is a better 
python is a netter

三、字符串的拼接

a = 'boy'
b = 'girl'
c = a + b
c
'boygirl'
'@@@@'.join([a,b])#效率比较高,用前面单引号的字符串拼接起来
'boy@@@@girl'

四、内置方法

  • count()
  • center()
  • startswith()
  • find()
  • format()
  • lower()
  • upper()
  • strip()
  • replace()
  • split()
str = 'hellow python'
str.count('l')#计算单个字符再字符串中的个数,统计元素个数
2
str.capitalize()#字符串的首字母大写
'Hellow python'
str.center(50,'*')#居中
'******************hellow python*******************'
str.endswith('y')# 看以某个内容结尾
False
str.startswith('he')
True
str2 = 'he\tllow python'
print(str.expandtabs(tabsize=20))
hellow python
str.find('p')#找寻元素返回索引值
7
str2 = 'hellow python {boy}'
str2.format(boy = 'bob')  
'hellow python bob'
str.index('p')
7
str3 = '123asf'
str3.isalnum()#判断是否有字母字符串
True
str3.isdecimal()#判断是否是十进制的数
False
str3.isdigit()#判断是否是数字
False
str3.isnumeric()
False
str3.isidentifier()#检验是否是非法变量
False
str3.islower()#判断是否都是小写
True
str3.isupper()#判断是否都是大写
False
str.isspace()#判断是否是空格
False
'My Python'.istitle()#判断是否是标题的格式
True
str3.lower()
'123asf'
str3.upper()
'123ASF'
'SFsdf'.swapcase()
'sfSDF'
str3.ljust(20,'@')
'123asf@@@@@@@@@@@@@@'
str3.rjust(20,'@')
'@@@@@@@@@@@@@@123asf'
print('      pythton'.strip())
pythton
str3.replace('123','456')#替换
'456asf'
str3.rfind('s')
4
'12 345 343'.split(' ')
['12', '345', '343']
'congage agege'.title()
'Congage Agege'

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值