Python 字符串的方法汇总

本文详细介绍了Python中字符串的各种常用方法,包括索引、切片、查找、替换、分割等,并通过实例展示了如何使用这些方法进行字符串处理。

不全面,以后会逐渐更新

# 字符串方法汇总
q = "hello world i want to change the world"
# 1.下标索引
print(q[4])
# o
# [Finished in 0.2s]
# 2. 切片:切片是指对操作的对象截取其中一部分的操作
print(q[::-1])
# dlrow eht egnahc ot tnaw i dlrow olleh
# [Finished in 0.1s]
# 3.find(rfind:右侧开始查找):检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1 mystr.find(str, start=0, end=len(mystr))
print(q.find('l',5,20))
# 9
# [Finished in 0.1s]
# 4.index(rindex:右侧开始):返回索引值mystr.index(str, start=0, end=len(mystr)) 
print(q.index("o",5,20))
# 7
# [Finished in 0.1s]
# 5.count:返回 str在start和end之间 在 mystr里面出现的次数 mystr.count(str, start=0, end=len(mystr))
print(q.count("o",5,25))
# 2
# [Finished in 0.1s]
# 6.replace:把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.mystr.replace(str1, str2,  mystr.count(str1))
print(q.replace("o","你",3))
# hell你 w你rld i want t你 change the world
# [Finished in 0.1s]
# 7.split:以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串  mystr.split(str=" ", 2) 
print(q.split("o",3))
# ['hell', ' w', 'rld i want t', ' change the world']
# [Finished in 0.1s]
# 8.capitalize:把字符串的第一个字符大写   mystr.capitalize()
print(q.capitalize())
# Hello world i want to change the world
# [Finished in 0.1s]
# 9.title:把字符串的每个单词首字母大写  mystr.title()
print(q.title())
# Hello World I Want To Change The World
# [Finished in 0.1s]
# 10.startswith(endwith:以什么结尾):检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False  mystr.startswith(obj)
print(q.startswith('hell'))
print(q.startswith('qell'))
# True
# False
# [Finished in 0.1s]
# 11.lower(upper:转换大写):转换 mystr 中所有大写字符为小写  mystr.lower() 
print(q.lower())
print(q.upper())
# hello world i want to change the world
# HELLO WORLD I WANT TO CHANGE THE WORLD
# [Finished in 0.1s]
# 12.ljust(rjust:右对齐;center:居中):返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串   mystr.ljust(width) 
print(q.rjust(70))
print(q.center(90))
                                # hello world i want to change the world
                          # hello world i want to change the world        
# [Finished in 0.1s]
# 13.strip(lstrip:左对齐;rstrip:右对齐):删除mystr字符串两端的空白字符  mystr.strip()
print("   hello   ".strip())
# hello
# [Finished in 0.1s]
# 14.partition(rpartition:右侧开始):把mystr以str分割成三部分,str前,str和str后  mystr.partition(str)
print(q.partition("world"))
# ('hello ', 'world', ' i want to change the world')
# [Finished in 0.1s]
# 15.splitlines:按照行分隔,返回一个包含各行作为元素的列表  mystr.splitlines() 
print("hello\nworld".splitlines())
# ['hello', 'world']
# [Finished in 0.1s]
# 16.join:mystr 中每个字符后面插入str,构造出一个新的字符串  mystr.join(str)
print("hello".join("你好吗"))
# 你hello好hello吗
# [Finished in 0.1s]
# 17.isalpha:如果 mystr 所有字符都是字母 则返回 True,否则返回 False   mystr.isalpha() 
# isdigit:如果 mystr 只包含数字则返回 True 否则返回 False.  mystr.isdigit() 
# isalnum:如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False  mystr.isalnum() 
print("asd".isalpha())
print("234".isalpha())
print("asd".isdigit())
print("234".isdigit())
# True
# False
# False
# True
# [Finished in 0.1s]

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值