周末回顾基本python数据类型之字符串详细用法~(可以收藏欧~)

首先我将为大家介绍字符串的常见方法

# 普通的索引操作想必大家都会,我就来讲解一些比较特殊的吧
s='abcd'
s=s[::-1]#倒着取
print(s)
# 输出结果:dcba

s=s[0:3:2]#[start:end:step] start:起始位置 end:结束位置(不包含最后一个)step:步长,如果步长为n,则在start:end范围内从开始位置依次隔n个取值
print(s)
# 输出结果:db

# 特殊情形
s=s[-1:-4:-2]# start,end,step均为'-'时
print(s)
'''
-4 -3 -2 -1
 d  c  b  a<--倒着取时,从-1开始取智
'''
# 输出结果:ac(注意欧倒着取)
# strip()# 去掉指定的字符
msg = '    ab   '
msg = msg.strip()  # 默认去掉空格  不会改变原值,会产生新值
print(msg)
# 输出结果:ab

msg = '******a*b****'
msg = msg.strip('*')#注意!只去左右,不去中间
print(msg)
# 输出结果:a*b

msg=msg='%&&**%$   ***ab*^$$#'.strip(' %^*$&#')# 会把指定的字符都去掉(除了中间的)
print(msg) 
# 输出结果:ab

# 切分 split:把一个字符按照某种分隔符进行切分,得到一个列表,默认按照空格分割
info='a b c'
res=info.split()# 默认去掉空格符
print(res)
# 输出结果 ['a', 'b', 'c']

info='a:b:c'
res=info.split(':')# 指定切割字符
print(res)
# 输出结果 ['a', 'b', 'c']

# 指定切割次数
info='a:b:c'
res=info.split(':',1)#只将第一个:位置与剩下位置切割开
print(res)
# 输出结果: ['a', 'b:c']

msg='AAbb'
print(msg.lower())#大写改为小写 -->aabb
print(msg.upper())#小写改为大写 -->AABB

msg='my name is dad'
print(msg.startswith('my'))# -->True
print(msg.endswith('dad'))# -->True

# join:把列表拼接成字符串
l=['a','b']
l=":".join(l)#按照某个分隔符,把元素全为字符串的列表拼接成一个字符串
print(l)# -->a:b

msg='you can you up'
print(msg.replace('you', 'You'))# -->You can You up
print(msg.replace('you', 'You',1))#指定replace取代个数-->You can you up

# 判断字符串是否由纯数字组成
print('123'.isdigit())# -->True
print('12.3'.isdigit())# -->False

msg='hello ab haha'
print(msg.find('e'))#返回要找到的字符串在大字符串中的起始索引,若找不到返回-1-->1
print(msg.index('e'))#若找不到则抛出异常-->1

print(msg.count('a'))#统计在字符串中出现的次数-->3


print('ab'.center(20,'*'))#居中显示,20代表字符串长度,在不够的位置用*
# -->*********ab*********
print('ab'.ljust(20,'*'))#左对齐
# -->ab******************
print('ab'.rjust(20,'*'))#右对齐
# -->******************ab
print('ab'.zfill(10))#用0填充
# -->00000000ab

msg='Hello\t world'
print(msg.expandtabs(2))#设置制表符代表的空格数为2
# -->Hello  world
print('hello word'.capitalize())#首字母大写
# -->Hello word
print('Hello Word'.swapcase())#大小写反转
# -->hELLO wORD
print('hello world'.title())#每个单词的首字母大写(以空格区分每个单词)
# -->Hello World
print('abc'.islower())#字母必须全为小写
# -->True
print('ABC'.isupper())#字母必须全为大写
# -->True
print('Abc Bc'.istitle())#字母是否首字母大写
# -->True
print('12234abc'.isalnum())#字符串是否由字母或数字组成
# -->True
print('abc'.isalpha())#由字母组成
# -->True
print('abc'.isspace())#是否全为空格
# -->False
print('age_of_you'.isidentifier())#名字是否合法
# -->True

num1=b'4'#bytes
num2=u'4'#unicode pyhon3中无需加u就是unicode
num3='四'#中文数字
num4='Ⅳ'#罗马数字

print(num1.isdigit())#True
print(num2.isdigit())#True
print(num3.isdigit())#False
print(num4.isdigit())#False

print(num2.isnumeric())#True
print(num3.isnumeric())#True
print(num4.isnumeric())#True

print(num2.isdecimal())#True
print(num3.isdecimal())#False
print(num4.isdecimal())#False

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值