日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
31個(gè)必備的Python字符串方法,建議收藏!

字符串是Python中基本的數(shù)據(jù)類(lèi)型,幾乎在每個(gè)Python程序中都會(huì)使用到它。

1、Slicing

slicing切片,按照一定條件從列表或者元組中取出部分元素(比如特定范圍、索引、分割值)

s = '   hello   '
s = s[:]
print(s)
# hello
s = ' hello '
s = s[3:8]
print(s)
# hello

2、strip()

strip()方法用于移除字符串頭尾指定的字符(默認(rèn)為空格或換行符)或字符序列。

s = '   hello   '.strip()
print(s)
# hello
s = '###hello###'.strip()
print(s)
# ###hello###

在使用strip()方法時(shí),默認(rèn)去除空格或換行符,所以#號(hào)并沒(méi)有去除。

可以給strip()方法添加指定字符,如下所示。

s = '###hello###'.strip('#')
print(s)
# hello

此外當(dāng)指定內(nèi)容不在頭尾處時(shí),并不會(huì)被去除。

s = ' \n \t hello\n'.strip('\n')
print(s)
#
# hello
s = '\n \t hello\n'.strip('\n')
print(s)
# hello

第一個(gè)\n前有個(gè)空格,所以只會(huì)去取尾部的換行符。

最后strip()方法的參數(shù)是剝離其值的所有組合,這個(gè)可以看下面這個(gè)案例。

s = 'www.baidu.com'.strip('cmow.')
print(s)
# baidu

最外層的首字符和尾字符參數(shù)值將從字符串中剝離。字符從前端移除,直到到達(dá)一個(gè)不包含在字符集中的字符串字符為止。

在尾部也會(huì)發(fā)生類(lèi)似的動(dòng)作。

3、lstrip()

移除字符串左側(cè)指定的字符(默認(rèn)為空格或換行符)或字符序列。

s = '   hello   '.lstrip()
print(s)
# hello

同樣的,可以移除左側(cè)所有包含在字符集中的字符串。

s = 'Arthur: three!'.lstrip('Arthur: ')
print(s)
# ee!

4、rstrip()

移除字符串右側(cè)指定的字符(默認(rèn)為空格或換行符)或字符序列。

s = '   hello   '.rstrip()
print(s)
# hello

5、removeprefix()

Python3.9中移除前綴的函數(shù)。

# python 3.9
s = 'Arthur: three!'.removeprefix('Arthur: ')
print(s)
# three!

和strip()相比,并不會(huì)把字符集中的字符串進(jìn)行逐個(gè)匹配。

6、removesuffix()

Python3.9中移除后綴的函數(shù)。

s = 'HelloPython'.removesuffix('Python')
print(s)
# Hello

7、replace()

把字符串中的內(nèi)容替換成指定的內(nèi)容。

s = 'string methods in python'.replace(' ', '-')
print(s)
# string-methods-in-python
s = 'string methods in python'.replace(' ', '')
print(s)
# stringmethodsinpython

8、re.sub()

re是正則的表達(dá)式,sub是substitute表示替換。

re.sub則是相對(duì)復(fù)雜點(diǎn)的替換。

import re
s = "string methods in python"
s2 = s.replace(' ', '-')
print(s2)
# string----methods-in-python
s = "string methods in python"
s2 = re.sub("\s+", "-", s)
print(s2)
# string-methods-in-python

和replace()做對(duì)比,使用re.sub()進(jìn)行替換操作,確實(shí)更高級(jí)點(diǎn)。

9、split()

對(duì)字符串做分隔處理,最終的結(jié)果是一個(gè)列表。

s = 'string methods in python'.split()
print(s)
# ['string', 'methods', 'in', 'python']

當(dāng)不指定分隔符時(shí),默認(rèn)按空格分隔。

s = 'string methods in python'.split(',')
print(s)
# ['string methods in python']

此外,還可以指定字符串的分隔次數(shù)。

s = 'string methods in python'.split(' ', maxsplit=1)
print(s)
# ['string', 'methods in python']

10、rsplit()

從右側(cè)開(kāi)始對(duì)字符串進(jìn)行分隔。

s = 'string methods in python'.rsplit(' ', maxsplit=1)
print(s)
# ['string methods in', 'python']

11、join()

string.join(seq)。以string作為分隔符,將seq中所有的元素(的字符串表示)合并為一個(gè)新的字符串。

list_of_strings = ['string', 'methods', 'in', 'python']
s = '-'.join(list_of_strings)
print(s)
# string-methods-in-python
list_of_strings = ['string', 'methods', 'in', 'python']
s = ' '.join(list_of_strings)
print(s)
# string methods in python

12、upper()

將字符串中的字母,全部轉(zhuǎn)換為大寫(xiě)。

s = 'simple is better than complex'.upper()
print(s)
# SIMPLE IS BETTER THAN COMPLEX

13、lower()

將字符串中的字母,全部轉(zhuǎn)換為小寫(xiě)。

s = 'SIMPLE IS BETTER THAN COMPLEX'.lower()
print(s)
# simple is better than complex

14、capitalize()

將字符串中的首個(gè)字母轉(zhuǎn)換為大寫(xiě)。

s = 'simple is better than complex'.capitalize()
print(s)
# Simple is better than complex

15、islower()

判斷字符串中的所有字母是否都為小寫(xiě),是則返回True,否則返回False。

print('SIMPLE IS BETTER THAN COMPLEX'.islower()) # False
print('simple is better than complex'.islower()) # True

16、isupper()

判斷字符串中的所有字母是否都為大寫(xiě),是則返回True,否則返回False。

print('SIMPLE IS BETTER THAN COMPLEX'.isupper()) # True
print('SIMPLE IS BETTER THAN complex'.isupper()) # False

17、isalpha()

如果字符串至少有一個(gè)字符并且所有字符都是字母,則返回 True,否則返回 False。

s = 'python'
print(s.isalpha())
# True
s = '123'
print(s.isalpha())
# False
s = 'python123'
print(s.isalpha())
# False
s = 'python-123'
print(s.isalpha())
# False

18、isnumeric()

如果字符串中只包含數(shù)字字符,則返回 True,否則返回 False。

s = 'python'
print(s.isnumeric())
# False
s = '123'
print(s.isnumeric())
# True
s = 'python123'
print(s.isnumeric())
# False
s = 'python-123'
print(s.isnumeric())
# False

19、isalnum()

如果字符串中至少有一個(gè)字符并且所有字符都是字母或數(shù)字,則返回True,否則返回 False。

s = 'python'
print(s.isalnum())
# True
s = '123'
print(s.isalnum())
# True
s = 'python123'
print(s.isalnum())
# True
s = 'python-123'
print(s.isalnum())
# False

20、count()

返回指定內(nèi)容在字符串中出現(xiàn)的次數(shù)。

n = 'hello world'.count('o')
print(n)
# 2
n = 'hello world'.count('oo')
print(n)
# 0

21、find()

檢測(cè)指定內(nèi)容是否包含在字符串中,如果是返回開(kāi)始的索引值,否則返回-1。

s = 'Machine Learning'
idx = s.find('a')
print(idx)
print(s[idx:])
# 1
# achine Learning
s = 'Machine Learning'
idx = s.find('aa')
print(idx)
print(s[idx:])
# -1
# g

此外,還可以指定開(kāi)始的范圍。

s = 'Machine Learning'
idx = s.find('a', 2)
print(idx)
print(s[idx:])
# 10
# arning

22、rfind()

類(lèi)似于find()函數(shù),返回字符串最后一次出現(xiàn)的位置,如果沒(méi)有匹配項(xiàng)則返回 -1。

s = 'Machine Learning'
idx = s.rfind('a')
print(idx)
print(s[idx:])
# 10
# arning

23、startswith()

檢查字符串是否是以指定內(nèi)容開(kāi)頭,是則返回 True,否則返回 False。

print('Patrick'.startswith('P'))
# True

24、endswith()

檢查字符串是否是以指定內(nèi)容結(jié)束,是則返回 True,否則返回 False。

print('Patrick'.endswith('ck'))
# True

25、partition()

string.partition(str),有點(diǎn)像find()和split()的結(jié)合體。

從str出現(xiàn)的第一個(gè)位置起,把字符串string分成一個(gè)3 元素的元組(string_pre_str,str,string_post_str),如果string中不包含str則 string_pre_str==string。

s = 'Python is awesome!'
parts = s.partition('is')
print(parts)
# ('Python ', 'is', ' awesome!')
s = 'Python is awesome!'
parts = s.partition('was')
print(parts)
# ('Python is awesome!', '', '')

26、center()

返回一個(gè)原字符串居中,并使用空格填充至長(zhǎng)度width的新字符串。

s = 'Python is awesome!'
s = s.center(30, '-')
print(s)
# ------Python is awesome!------

27、ljust()

返回一個(gè)原字符串左對(duì)齊,并使用空格填充至長(zhǎng)度width的新字符串。

s = 'Python is awesome!'
s = s.ljust(30, '-')
print(s)
# Python is awesome!------------

28、rjust()

返回一個(gè)原字符串右對(duì)齊,并使用空格填充至長(zhǎng)度width的新字符串。

s = 'Python is awesome!'
s = s.rjust(30, '-')
print(s)
# ------------Python is awesome!

29、f-Strings

f-string是格式化字符串的新語(yǔ)法。

與其他格式化方式相比,它們不僅更易讀,更簡(jiǎn)潔,不易出錯(cuò),而且速度更快!

num = 1
language = 'Python'
s = f'{language} is the number {num} in programming!'
print(s)
# Python is the number 1 in programming!
num = 1
language = 'Python'
s = f'{language} is the number {num*8} in programming!'
print(s)
# Python is the number 8 in programming!

30、swapcase()

翻轉(zhuǎn)字符串中的字母大小寫(xiě)。

s = 'HELLO world'
s = s.swapcase()
print(s)
# hello WORLD

31、zfill()

string.zfill(width)。

返回長(zhǎng)度為width的字符串,原字符串string右對(duì)齊,前面填充0。

s = '42'.zfill(5)
print(s)
# 00042
s = '-42'.zfill(5)
print(s)
# -0042
s = '+42'.zfill(5)
print(s)
# +0042


分享文章:31個(gè)必備的Python字符串方法,建議收藏!
分享URL:http://www.5511xx.com/article/cdpcoid.html