Inventory of 16 common operations methods on Python strings

Release: 2023-07-25 15:30:36
forward
2215 people have browsed it

1. Common operations

Take the string

'lstr = 'welcome to Beijing Museumitcpps fdsfs'
Copy after login

as an example, Introduces common operations on characters.

<1> 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))
Copy after login

Example:

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


print(lstr.find("dada"))
Copy after login

operation result:

Inventory of 16 common operations methods on Python strings


<2> 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))
Copy after login

例:

lstr = 'welcome to Beijing Museumitcpps fdsfs'


print(lstr.index("dada"))
Copy after login

运行结果:

Inventory of 16 common operations methods on Python strings


<3> count

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

语法:

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

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.count("s"))
Copy after login

运行结果:

Inventory of 16 common operations methods on Python strings


<4> replace

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

1str.replace(str1, str2,  1str.count(str1))
Copy after login

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.replace("s", "ttennd"))
Copy after login

运行结果:

Inventory of 16 common operations methods on Python strings


<5> split

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

1str.split(str=" ", 2)
Copy after login

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.split("to", 5))
Copy after login

运行结果:

Inventory of 16 common operations methods on Python strings


<6> capitalize

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

lstr.capitalize()
Copy after login

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.capitalize())
Copy after login

运行结果:

Inventory of 16 common operations methods on Python strings


<7> title

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

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

<8> startswith

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

1str.startswith(obj)
Copy after login

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.startswith('we'))
Copy after login

运行结果:

Inventory of 16 common operations methods on Python strings


<9> endswith

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

1str.endswith(obj)
Copy after login

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.endswith('hfs'))
Copy after login

运行结果:

Inventory of 16 common operations methods on Python strings


<10> lower

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

1str.lower()
Copy after login

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.lower())
Copy after login

运行结果:

Inventory of 16 common operations methods on Python strings


<11> upper

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

1str.upper()
Copy after login

例:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'


print(lstr.upper())
Copy after login

运行结果:

Inventory of 16 common operations methods on Python strings


<12> strip

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

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

<13> rfind

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

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

例:

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

运行结果:

Inventory of 16 common operations methods on Python strings


<14> rindex

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

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

例:

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

运行结果:

Inventory of 16 common operations methods on Python strings


<15> partition

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

1str.partition(str)
Copy after login

例:

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

运行结果:

Inventory of 16 common operations methods on Python strings


<16> join

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

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

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!

Related labels:
source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!