• 手机站
  • 收藏
聚培教育网合作机构 > 大连达内教育
大连达内教育
400-998-6158
大连达内教育是一家由留学海归创办的高端职业教育培训机构,是中国人才培养平台、人才输送平台。
大连达内教育

Python基础知识字符串学习方法有哪些

python学习网

更新时间:2021-10-28 浏览:82
核心提示:Python基础知识字符串学习方法1.大小写转换

Python基础知识字符串学习方法有哪些

1.大小写转换

方式 lower( ).方式 upper( )

>>>S='Life is simple, I use Python'

>>>S.lower()

'life is simple, i use python'

>>>S.upper()

'LIFE IS SIMPLE, I USE PYTHON'

回到S字符串数组的小写字母.英文大写文件格式。(留意,这也是新产生的字符串数组,在另一片运行内存精彩片段中)

>>> print('ab ABC'.lower())

ab abc

>>> print('ab CD'.upper())

AB CD

方式 title( ),将每一个英语单词的首字母大写

>>> print('sherlock holmes'.title())

Sherlock Holmes

2.字符串检索

方式 cout( )

S.count(sub[,start[, end]])

回到字符串数组S中字符串函数sub发生的频次,能够特定从哪里逐渐测算(start)及其测算到哪里完毕(end),数据库索引从0逐渐测算,不包括end界限。

>>> print('brubrubr'.count('br'))

3

>>> print('brubrubr'.count('br',1))

2 #index=1,即从‘r’开始查找,搜索范畴为‘rubrumm’

>>> print('brubrubr'.count('br',1,7))

1 #搜索范畴为‘rubrub’

>>> print('brubrubr'.count('br',1,8))

2 #搜索范畴为‘rubrubr’

方式 endwith( ).方式 startwith( )

S.endswith(suffix[, start[, end]])

S.startswith(prefix[, start[, end]])

endswith() 查验字符串数组S是不是以suffix末尾,回到布尔值的True和False。suffix能够是一个元组(tuple)。能够特定起止start和末尾end的检索界限。

同样 startswith() 用于分辨字符串数组S是不是以prefix开始。

>>> print('abcdef'.endswith('def'))

True #检索范畴为‘abcdef’

>>> print('abcdef'.endswith('def',4))

False #检索范畴为‘ef’

>>> print('abcdef'.endswith('def',0,5))

False #检索范畴为‘abcde’

>>> print('abcdef'.endswith('def',0,6))

True #检索范畴为‘abcdef’

方式 find( ).方式 rfind( ) & 方式 index( ).方式 rindex( )

S.find(sub[, start[, end]])

S.rfind(sub[, start[, end]])

S.index(sub[, start[, end]])

S.rindex(sub[, start[, end]])

方式 find( )检索字符串数组S中能否包括字符串函数sub,假如包括,则回到sub的数据库索引部位,不然回到"-1"。能够特定起止start和完毕end的查找部位。

方式 index( )和方式 find( )一样,**不同之处取决于当找不着字符串函数时,抛出去 ValueError 不正确。

方式 rfind( )则是回到检索到的最右侧字符串函数的部位,假如只检索到一个或沒有检索到字符串函数,则和方式 find( )是等额的的。

方式 rindex( )同理可知。

3.拼凑字符串数组


Python中运用” “来合拼字符串数组的方式 称为拼凑。

>>> print('abc' 'def')

abcdef

你是否还记得Python中一个最經典的事例——”hello world“,大家对于此事做一个小扩展,能够輸出一个” hello xxxx“的輸出。

>>> name='sherlock holmes'

>>> print("Hello," name.title() "!")

Hello,Sherlock Holmes!

4.切分

方式 split( ).方式 rsplit( ).方式 splitlines( )

S.split(sep=None, maxsplit=-1)

S.rsplit(sep=None, maxsplit=-1)

S.splitlines([keepends=True])

这三个涵数用于分割字符串,并形成一个目录。下边让我们依据事例来了解一下三个涵数的使用方法。

先看来方式 split( )的使用方法:

>>> 'a,b,c'.split(',')

['a', 'b', 'c']

>>> 'a,b,c'.split(',',1)

['a', 'b,c'] #限制切分频次1

>>> 'a b c'.split(maxsplit=1)

['a', 'b c'] #maxsplit限制切分频次

>>> 'a,b,c'.split()

['a,b,c']

>>> 'a b c'.split()

['a', 'b', 'c'] #不特定sep时

方式 split() 依据sep对S开展切分,maxsplit用以特定切分频次,如果不特定sep或是特定为None,则更改切分优化算法:以空格符为分节符。

方式 rsplit( )和方式 split()是一样的,只*是是以右侧向左侧检索。

下边一起来看看方式 splitlines( ),它是来专业用于切分回车符。

>>> ''.split('n')

['']

>>> ''.splitlines()

[]

>>> 'OH MY GODn'.split('n')

['OH MY GOD', '']

>>> 'OH MY GODn'.splitlines()

['OH MY GOD']

5.加上空缺

n 回车符

>>> print("Films:nX-MennWondernMarvel's The Avengers")

Films:

X-Men

Wonder

Marvel's The Avengers

nt 回车符和制表符

>>> print("Films:ntX-MenntWonderntMarvel's The Avengers")

Films:

X-Men

Wonder

Marvel's The Avengers

6.剪修

方式 strip( ).方式 lstrip( ).方式 rstrip( )

S.strip([chars]) #清除上下两侧的标识符chars,默认设置清除空缺(空格符.制表符.回车符)。

S.lstrip([chars]) #清除左侧

S.rstrip([chars]) #清除右侧

下边来说一下详细的事例:

>>> ' sherlock holmes '.lstrip()

'sherlock holmes '

>>> ' sherlock holmes '.rstrip()

' sherlock holmes'

>>> ' sherlock holmes '.strip()

'sherlock holmes'

>>> 'sherlock holmes'.lstrip('s')

'herlock holmes'

>>> 'sherlock holmes'.rstrip('s')

'sherlock holme'

>>> 'sherlock holmes'.strip('s')

'herlock holme'

此外,chars能够是好几个标识符编码序列。在清除时,只需是这一编码序列中的标识符,都是会被清除。

>>> print('www.example.com'.lstrip('cmowz.'))

example.com

>>> print('wwwz.example.com'.lstrip('cmowz.'))

example.com

>>> print('wwaw.example.com'.lstrip('cmowz.'))

aw.example.com

>>> print('www.example.com'.strip('cmowz.'))

example

因为 www.example.com 的前4字符全是标识符编码序列 cmowz. 中的标识符,因此都被清除,而第五个标识符e没有标识符编码序列中,因此剪修告一段落。同样 wwwz.example.com ;wwaw.example.com 中第3字符a并不是标识符编码序列中的标识符,因此剪修告一段落。

更多>同类资讯
更多>相关课程
顶部