导语:
本文主要介绍了关于Python字符串方法如何使用的相关知识,包括java字符串方法,以及字符串是什么这些编程知识,希望对大家有参考作用。
1、find方法可以在一个较长的字符串中查找子串
。
返回子字符串所在的最左边的索引,如果没有找到则返回 -1
a = 'abcdefghijk'
print(a.find('abc')) #the result : 0
print(a.find('abc',10,100)) #the result : 11 指定查找的起始和结束查找位置
2、join方法是非常重要的字符串方法。
是split方法的逆方法
,用来连接序列中的元素,并且需要被连接的元素都必须是字符串。
a = ['1','2','3']
print('+'.join(a)) #the result : 1+2+3
3、split是join的逆方法。
用来将字符串分割成序列
print('1+2+3+4'.split('+')) #the result : ['1', '2', '3', '4']
4、strip方法返回去除首位空格。
print(" test test ".strip()) #the result :“test test”
replace方法返回某字符串。
所有匹配项均被替换之后得到字符串。
print("This is a test".replace('is','is_test')) #the result : This_test is_test a test
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ Python中的垃圾回收机制是什么11/15
- ♥ Python中绝对引用和相对引用的区别12/28
- ♥ python中main函数是什么意思08/17
- ♥ python中repr函数是如何使用的?11/20
- ♥ 如何下载python spyder09/04
- ♥ 是python跨平台吗08/25
内容反馈