python字符串实例_python 字符串内置方法实例

一.字符串方法总结:

1.查找:

find(rfind)、index(rindex)、count

2.变换:

capitalize、expandtabs、swapcase、title、lower、upper、strip(rstrip/lstrip)(析取)、zfill

3.分行和折行:

join、split(rsplit)、splitlines、partion(rpartion)

4.替换和高级替换:

replace、translate、format

5.位置:

center、rjust(ljust)、zfill(左边0填充)

5.is测试:

startswith(endswith)、isalnum、isalpha、isdigit、islower、isupper、isspace、istitle

二.字典方法总结:

字典的键值只能是不可变对象。

1.获取:

get、setdefault(获取或创建)、items、keys、values、

2.创建:

copy(浅复制)、fromkeys、update(更新)

3.删除:

clear、pop、popitem

三. 列表方法总结:

1.增加:

append、extend、insert、、

2.删除:

pop、remove、

3.排序VS查找VS统计:

sort、reverse、index、count、

四.可变集合set方法总结:

1.标准操作:

difference(差)、intersection(交)、union(并)、symmetric_difference(对称差)、isdisjoint、issubset、issuperset、

2.增加:

add

3.更新:

update(t) t可以是任意的可迭代类型 ,还有一些差更新difference_update,对称差symmetric_difference更新,intersection_difference交更新

4.删除:

clear、discard、pop、remove(若无返回KeyError)

str = 'hello aaron'

strA = 'hello agu AARON'

str.capitalize()  # 字符串首字母大写。 输出 'Hello aaron'

str.title()  # 每个单词的首字母都大写。 输出 'Hello Aaron'

str.upper()  # 每个字符都大写。 输出 'HELLO AARON'

strA.lower()  # 每个字符都小写。 输出 'hello agu aaron'

strA.swapcase()  #大小写转换。 输出 'HELLO AGU aaron'

str.center(14)  #字符串居中不够14个字符的前后补空格(先后再前)。输出 ' hello aaron '

str.ljust(12)  #左对齐不够12个字符的右补空格。 输出 'hello aaron '

str.rjust(12)  #右对齐不够12个字符的左补空格。 输出 ' hello aaron'

str.ljust(20).rstrip()  #rstrip 剥除字符串右边所有空格。 输出 'hello aaron'

str.rjust(20).lstrip()  #左剥除

str.center(20).strip()  #前后都剥

strN = ' '

strN.isspace()  #是否所有字符为空格。 输出 True

strTab = 'HELLO\tworld'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值