最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中字符串的常用方法總結(jié)

 更新時(shí)間:2022年12月06日 11:29:02   作者:Mrwhite86  
字符串是?字符的序列?。字符串基本上就是一組單詞。我?guī)缀蹩梢员WC你在每個(gè)Python程序中都要用到字符串。本文為大家總結(jié)了15個(gè)常用的方法,希望對(duì)大家有所幫助

1、strip()、lstrip()、rstrip()

作用:去除兩邊空格、左邊空格、右邊空格

s = "   abcd   "
print("|"+s.strip()+"|")
print("|"+s.lstrip()+"|")
print("|"+s.rstrip()+"|")

查看運(yùn)行結(jié)果:

2、removeprefix()、removesuffix()

作用:移除前綴、移除后綴

s = "hello:world"
print(s.removeprefix("hello"))
print(s.removesuffix("world"))

查看運(yùn)行結(jié)果:

3、replace()

作用:替換字符串中的內(nèi)容替換成指定的內(nèi)容

s = "hello:world"
s = s.replace(":", "-")
print(s)

查看運(yùn)行結(jié)果:

4、split()、rsplit()

作用:從左邊起根據(jù)對(duì)用的內(nèi)容分割字符串、從右邊起根據(jù)對(duì)用的內(nèi)容分割字符串(當(dāng)指定字符串的分隔次數(shù)時(shí)存在區(qū)別)

s = "hello:world:ok"
print(s.split(":"))
print(s.rsplit(":"))
print(s.split(":", maxsplit=1))
print(s.rsplit(":", maxsplit=1))

查看運(yùn)行結(jié)果:

5、join()

作用:將括號(hào)內(nèi)的元素(均需要滿足字符串格式)合并成一個(gè)新的字符串,已join前的字符作為分隔符

l = ["hello", "1", "world"]
print("".join(l))
print("-".join(l))

查看運(yùn)行結(jié)果:

6、upper()、lower()、capitalize()

作用:將所有字母轉(zhuǎn)為大寫、將所有字母轉(zhuǎn)為小寫、將首字母轉(zhuǎn)為大寫

s = "helloWORLD"
print(s.upper())
print(s.lower())
print(s.capitalize())

查看運(yùn)行結(jié)果:

7、islower()、isupper()、isalpha()、isnumeric()、isalnum()

作用:檢查字符串中字母是否都為小寫、檢查字符串中字母是否都為大寫、檢查字符串中字符是否都是字母、檢查字符串中字符是否都是數(shù)字、檢查所有的字符串是否都是數(shù)字或字母

s1 = "helloworld"
s2 = "OK"
s3 = "hello OK"
s4 = "567"
s5 = "hello123"

print(s1.islower())
print(s2.isupper())
print(s3.islower(), s3.isupper())
print(s1.isalpha())
print(s4.isnumeric())
print(s5.isalpha(), s5.isnumeric())
print(s5.isalnum())
print(s3.isalnum())

查看運(yùn)行結(jié)果:

8、count()

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

s = "hello world"
print(s.count("o"))

查看運(yùn)行結(jié)果:

9、find()、rfind()

作用:返回字符串中是否包含指定內(nèi)容的索引信息(從左邊開始第一個(gè)出現(xiàn)的),不包含時(shí)返回-1、返回字符串中是否包含指定內(nèi)容的索引信息(從右邊開始第一個(gè)出現(xiàn)的),不包含時(shí)返回-1

s = "hello world"
print(s.find("x"))
print(s.find("o"))
print(s.rfind("o"))

查看運(yùn)行結(jié)果:

10、startswith()、endswith()

作用:檢查字符串是否是以指定內(nèi)容開頭、檢查字符串是否是以指定內(nèi)容結(jié)束

s = "hello world"
print(s.startswith("h"), s.endswith("h"))
print(s.startswith("d"), s.endswith("d"))

查看運(yùn)行結(jié)果:

11、partition()

作用:有點(diǎn)像find()和split()的結(jié)合體。將字符串根據(jù)指定的內(nèi)容拆分為三個(gè)元素的元祖,其中第二個(gè)元素為指定的內(nèi)容,如果不包含指定的內(nèi)容的話,返回的第一個(gè)元素為原字符串

s = "hello world"
print(s.partition(" "))
print(s.partition("hello"))
print(s.partition("123"))

查看運(yùn)行

12、center()、ljust()、rjust()

作用:

返回一個(gè)原字符串居中,并使用指定內(nèi)容(默認(rèn)為空格)填充至長(zhǎng)度width的新字符串

返回一個(gè)原字符串左對(duì)齊,并使用指定內(nèi)容(默認(rèn)為空格)填充至長(zhǎng)度width的新字符串

返回一個(gè)原字符串右對(duì)齊,并使用指定內(nèi)容(默認(rèn)為空格)填充至長(zhǎng)度width的新字符串。

s = "python"
print(s.center(30))
print(s.center(30, "-"))
print(s.ljust(30, "-"))
print(s.rjust(30, "-"))

查看運(yùn)行結(jié)果:

13、字符串專用f表達(dá)式

作用:是格式化字符串的新語(yǔ)法,更易讀,更簡(jiǎn)潔,不易出錯(cuò),而且速度更快!需要python3.6+的版本支持

name = "bk"
age = 15
print(f"my name is {name},and i am {age} years old!")

查看運(yùn)行結(jié)果:

14、swapcase()

作用:翻轉(zhuǎn)字符串中的字母大小寫

name = "My Name is Mr.white"
print(name.swapcase())

查看運(yùn)行結(jié)果:

15、zfill()

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

print("100".zfill(5))
print("+100".zfill(5))
print("-100".zfill(5))
print("+0010".zfill(5))

查看運(yùn)行結(jié)果:

到此這篇關(guān)于Python中字符串的常用方法總結(jié)的文章就介紹到這了,更多相關(guān)Python字符串方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

渭源县| 抚顺县| 特克斯县| 呼图壁县| 西乌珠穆沁旗| 孝感市| 长治县| 长葛市| 乐清市| 绥芬河市| 威远县| 宝丰县| 楚雄市| 拜城县| 浏阳市| 丰顺县| 柳州市| 保山市| 安徽省| 阿勒泰市| 华容县| 东阿县| 余庆县| 铁岭县| 綦江县| 交口县| 青海省| 来宾市| 仪征市| 河间市| 蓬莱市| 怀仁县| 柘城县| 鄂温| 苍南县| 荆州市| 稻城县| 马公市| 泊头市| 象山县| 磐安县|