Python基礎-字串,python基礎字串

來源:互聯網
上載者:User

Python基礎-字串,python基礎字串
字串的建立

s = "Hello World"
字串常用操作

join()  將序列中的元素以指定的字元串連產生一個新的字串

>>> s = ["1", "2", "3", "4"]>>> ".".join(s)1.2.3.4

strip()  移除字串頭尾指定的字元(預設為空白格)

>>> s = " Hello World ">>> s.strip()Hello World
>>> s = "*Hello World&"
>>> s.strip("*")
Hello World&

lstrip()  截掉字串左邊的空格或指定字元

>>> s = "*Hello World*">>> s.lstrip("*")Hello World*

index(str, beg=0, end=len(string))  檢測字串中是否包含子字串 str,不存在則報錯(beg:開始位置,預設為0;end:結束位置,預設為字串字最後一個位置)

>>> s = "Hello World">>> s.index("e")1>>> s.index("z")ValueError:substring not found

rindex()  返回字串最後一次出現的位置,如果沒有則報錯

>>> s = "Hello World">>> s.rindex("o")7

find()  檢測字串中是否包含子字串 str,不存在返回-1

>>> s = "Hello World">>> s.find("e")1>>> s.find("z")-1

rfind()  返回字串最後一次出現的位置,如果沒有匹配項則返回-1

split(str="", num=string.count(str))  通過指定分隔字元對字串進行切片(str:指定字串,預設為所有Null 字元;num:分隔次數),返回列表

>>> s = "www.luffycity.com">>> s.split(".")['www', 'luffycity', 'com']

splitlines()  按照行('\r', '\r\n', \n')分隔,返回一個包含各行作為元素的列表

>>> s = """luffycitynamei""">>> s.splitlines()['luffycity', 'namei']

count(sub, start= 0,end=len(string))  統計字串裡某個字元出現的次數(sub:統計的字串)

>>> s = "Hello World">>> s.count("o", 2, 9)2

replace(old, new[, max])  將字串中的子字串替換(old:舊字串;new:新字串;max:替換最大次數)

>>> s = "Hellz Wzrld">>> s.replace("z", "o")Hello World

endswith()  是否以指定尾碼結尾

>>> s = "Hello World">>> s.endswith("d")True>>> s.endswith("l")False

startswith()  是否以指定字串開頭

>>> s = "Hello World">>> s.startswith("H")True>>> s.startswith("e")False

isdigit()  是否為數字

>>> s = "12345">>> s.isdigit()True>>> s = "asdfg">>> s.isdigit()False

isalnum()  是否由字母和數字組成

isalpha()  是否只由字母組成

isdecimal()  是否只包含十進位字元

isidentifier()  是否包含該語言的保留字

islower()  是否由小寫字母組成

isprintable()  是否可列印

isspace()  是否只由空白字元組成

istitle()  單詞拼字首字母是否為大寫,且其他字母為小寫

title()  方法返回"標題化"的字串,所有單詞都是以大寫開始,其餘字母均為小寫

>>> s = "hello world">>> s.title()Hello World

isupper()  所有的字母是否都為大寫

upper()  將小寫字母轉換成大寫字母

>>> s = "Hello World">>> s.upper()HELLO WORLD

capitalize()  將第一個字母大寫,其他字母小寫

1 >>> s='hello world'2 >>> s.capitalize()3  'Hello world'

casefold()  將大寫字母轉換成小寫字母

>>> s='Hello World'>>> s.casefold() 'hello world'

lower()  字串中所有大寫字元為小寫,只適用於‘A~Z’

swapcase()  大小寫字母進行轉換

>>> s = "Hello World">>> s.swapcase()hELLO wORLD

center(width[, fillchar]) 如果 width 小於字串寬度直接返回字串,否則使用 fillchar 去填充(width:指定寬度;fillchar:填充字元)

>>> s = "Hello World">>> s.center(25, "*")*******Hello World*******       

ljust(width[, fillchar])  返回一個原字串靠左對齊,並使用空格填充至指定長度的新字串。如果指定的長度小於原字串的長度則返回原字串(width:指定寬度;fillchar:填充字元)

>>> s = "Hello World">>> s.ljust(25, "*")Hello World**************

rjust(width[, fillchar])  返回一個原字串靠右對齊,並使用空格填充至長度 width 的新字串。如果指定的長度小於字串的長度則返回原字串

>>> s = "Hello World">>> s.ljust(25, "*")**************Hello World

edcode()  以指定的編碼格式編碼字串

>>> s = "Hello World">>> s.encode(encoding="utf-8")b'Hello World

expandtabs()  把字串中的 tab 符號('\t')轉為空白格,預設為8

>>> s = "\t Hello World \t">>> s.expandtabs(16)                 Hello World    

partition()  返回一個3元的元組,第一個為分隔字元左邊的子串,第二個為分隔字元本身,第三個為分隔字元右邊的子串

>>> s = "Hello.World">>> s.partition(".")('Hello', '.', 'World')

zfill()  返回指定長度的字串,原字串靠右對齊,前面填充0

>>> s = "Hello World">>> s.zfill(25)00000000000000Hello World

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.