Home >Backend Development >Python Tutorial >Inventory of 16 common operations methods on Python strings

Inventory of 16 common operations methods on Python strings

Go语言进阶学习
Go语言进阶学习forward
2023-07-25 15:30:362406browse

1. Common operations

Take the string

'lstr = 'welcome to Beijing Museumitcpps fdsfs'

as an example, Introduces common operations on characters.

f35d6e602fd7d0f0edfa6f7d103c1b57 find

Check whether str is included in lstr, if so, return the starting index value, otherwise return -1.

Grammar:

lstr.find(str, start=0, end=len(lstr))

Example:

lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.find("Museum"))


print(lstr.find("dada"))

operation result:

Inventory of 16 common operations methods on Python strings


2cc198a1d5eb0d3eb508d858c9f5cbdb index

The same as the find() method, except that if str is not in lstr, an exception will be reported.

grammar:

lstr.index(str, start=0, end=len(lstr))

例:

lstr = 'welcome to Beijing Museumitcpps fdsfs'


print(lstr.index("dada"))

运行结果:

Inventory of 16 common operations methods on Python strings


5bdf4c78156c7953567bb5a0aef2fc53 count

返回 str在start和end之间 在 lstr里面出现的次数

语法:

lstr.count(str, start=0, end=len(lstr))

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.count("s"))

运行结果:

Inventory of 16 common operations methods on Python strings


23889872c2e8594e0f446a471a78ec4c replace

把 lstr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.

1str.replace(str1, str2,  1str.count(str1))

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.replace("s", "ttennd"))

运行结果:

Inventory of 16 common operations methods on Python strings


43ad812d3a971134e40facaca816c822 split

以 str 为分隔符切片 lstr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串

1str.split(str=" ", 2)

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.split("to", 5))

运行结果:

Inventory of 16 common operations methods on Python strings


efbfa0de8737dc86eae413541a49df20 capitalize

把字符串的第一个字符大写。

lstr.capitalize()

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.capitalize())

运行结果:

Inventory of 16 common operations methods on Python strings


40107655ec554331c1c6222ab67a141c title

把字符串的每个单词首字母大写。

>>> a = "hello itcast"
>>> a.title()
'Hello Itcast' #运行结果

37cd6113a8c348d99fa846f2c6fcea98 startswith

检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False

1str.startswith(obj)

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.startswith('we'))

运行结果:

Inventory of 16 common operations methods on Python strings


c161494dba5e0dd0fb25d890c74e408d endswith

检查字符串是否以obj结束,如果是返回True,否则返回 False.

1str.endswith(obj)

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.endswith('hfs'))

运行结果:

Inventory of 16 common operations methods on Python strings


eebe431eeb58984ec8915354762c30c6 lower

转换 lstr 中所有大写字符为小写

1str.lower()

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.lower())

运行结果:

Inventory of 16 common operations methods on Python strings


8494a7152f0ce9541779ac435cbe6aab upper

转换 lstr 中的小写字母为大写

1str.upper()

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.upper())

运行结果:

Inventory of 16 common operations methods on Python strings


8141c42af04c24b6c356713ee262f06a strip

删除lstr字符串两端的空白字符。

>>> a = "\n\t itcast \t\n"
>>> a.strip()
'itcast'  #运行结果

686111046a42eeee58032dc06d5f19ff rfind

类似于 find()函数,不过是从右边开始查找。

1str.rfind(str, start=0,end=len(1str) )

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.rfind('eijing'))

运行结果:

Inventory of 16 common operations methods on Python strings


636f057c96f4f4778f19692abc05b2ee rindex

类似于 index(),不过是从右边开始。

1str.rindex( str, start=0,end=len(1str))

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.rindex('eijing'))

运行结果:

Inventory of 16 common operations methods on Python strings


96da73a672242ebd5ff412f183fa77ab partition

把lstr以str分割成三部分,str前,str和str后。

1str.partition(str)

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.partition('eijing'))

运行结果:

Inventory of 16 common operations methods on Python strings


600cc0b653b1411e40248dd4d217d958 join

mystr 中每个字符后面插入str,构造出一个新的字符串。

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
str='233'
lstr.join(str)
li=["my","name","is","LY"]
print(str.join(li))
运行结果:

Inventory of 16 common operations methods on Python strings


二、总结

本文详细的讲解了Python基础 ( 字符串 )。介绍了有关字符串,切片的操作。下标索引。以及在实际操作中会遇到的问题,提供了解决方案。希望可以帮助你更好的学习Python。

The above is the detailed content of Inventory of 16 common operations methods on Python strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete