python字符串对象详解

字符串对象:

​ 在python中,字符串是一种基本数据类型。

​ 字符串的表达方式

'字符串'
"字符串"
"""字符串"""
'''字符串'''
dir(str)
['capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

以is开头的是判断字符串符合某些规则

c = 'i love python'

d = 'I LOVE PYTHON'

a = 'I Love Python'

isupper:判断字符串是否为大写

a.isupper()
d.isupper()
false
true

islower:判断字符串是否为小写

>>> c.islower()
True
>>> a.islower()
False

lower(): 将所有字母转化为小写

>>> a.lower()
'i love python'

upper(): 将所有字母转化为大写

capitalize():首字母大写

>>> c.capitalize()
'I love python'

center(): 居中对齐 s.center(20,‘*’) // 占20个字符居中对齐,剩下的由*填充

>>> a.center(20)
'   I Love Python    '
>>> a.center(20,'*')
'***I Love Python****'

ljust(): 左对齐

rjust(): 右对齐

>>> a.rjust(20)
'       I Love Python'

count(): 统计字符出现的次数

>>> a.count('o')
2
>>> a.count('x')
0

index(): 找到第一个出现的字符并返回索引,如果不存在则会报错。

>>> a
'I Love Python'
>>> a.index('o')
3
>>> a.rindex('o')
11

find(): 找到第一个出现的字符并返回索引,如果不存在则返回-1

>>> a.find('o')
3
>>> a.rfind('o')
11

rindex(): 从右向左找

rfind(): 从右向左找

startswith(): 判断以什么开始

>>> a.startswith('I')
True

endswith(): 判断以什么结束

>>> a.endswith('on')
True
>>> a.endswith('ion')
False

isalnum(): 判断是否由大小写和数字组成

isalpha(): 判断是否为字母组成

isascii(): 是不是在ascii码表中

isdecimal(): 检查字符串是否只包含十进制字符

str = u"this2009";  
print str.isdecimal();

str = u"23443434";
print str.isdecimal();
#输出结果
False
True

isdigit(): 判断是否只有数字组成。

isidentifier(): 判断命名是否有效(由字母和下划线开头,由字母数字和下划线组成)

encode(): 编码 将字符串转化为字节数据 str.encode(‘utf-8’) 字节(01二进制) 字符串

str.encode('utf-8')  	

decode(): 解码 将字节数据转化为字符串

join(): 拼接字符串 拼接字符串

>>> s = ('1','2','3')
>>> print('-'.join(s))
1-2-3

split(): 按特定的规则(默认以空格分割)分割字符串

例如 url=“www.baidu.com” url.split(‘.’) 按照点划分以列表形式返回,以变量中存在的字符进行分割,如果没有存在对应的字符则将整体通过列表返回。

>>> o = 'ababababab'
>>> o.split('.')
['ababababab']
>>> o.split('b')
['a', 'a', 'a', 'a', 'a', '']
>>> o.split('b', maxsplit=1)
['a', 'cbdbebfb']
>>> o.split('b', 1)    //设置只分割一次
['a', 'cbdbebfb']

rsplit(): 从右往左分割

strip(): 只能剔除两边的空格

>>> oo = '   s   '
>>> oo.strip()
's'
>>> oo.rstrip()
'   s'
>>> oo.lstrip()
's   '

rstrip(): 只剔除右边的空格

lstrip(): 只剔除左边的空格

replace(): 替换字符

a = this is apple ,this is apple!!!
>>> a.replace('is','was')
'thwas was apple ,thwas was apple!!!'
>>> a.replace('is','was',3)
'thwas was apple ,thwas is apple!!!'

title(): 返回标题化,将所有字母的首字母都转化为大写(非字母后的第一个字母将转换为大写字母)

txt = "hello b2b2b2 and 3g3g3g"
x = txt.title()
print(x)
#输出结果为:
Hello B2B2B2 And 3G3G3G

zfill():返回指定长度的字符串

str = 'hello python'
str.zfill(20)  
>>>'00000000hello python'   //原字符串右对齐,并以0填充
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值