Python中的字符串部分()

| | | | |

partition() 方法在第一次出現分隔符時拆分字符串,並返回一個元組,該元組包含分隔符之前的部分、分隔符和分隔符之後的部分。這裡的分隔符 —它是一個有參數的字符串。

語法:
string.partition(分隔符)

參數:
partition() 方法將一個分隔符(一個字符串)作為參數,在字符串第一次出現時對其進行分隔。

返回:
如果在字符串中找到分隔符參數,則返回一個元組,其中包含分隔符之前的部分、分隔符參數和分隔符之後的部分。
如果分隔符參數為,則返回一個包含字符串本身和兩個空字符串的元組沒有找到。

CODE 1


string = "pawan 是個好東西"


# '是 '找到分隔符

print (string.partition ( `is` ))


分隔符 # 未找到

print (string.partition ( `bad` ))

string = "pawan 很好,不是嗎"


# 當 &# 時拆分39;是 '

print (string.partition ( `是` ))

輸出:

(`pawan `, `is`, `a good`) (` pawan is a good`, ` `,` `) (` pawan `,` is`, "一個好,不是嗎") 

Codex2


string = "geeks is a good"


# '是 '找到分隔符

print (string.partition ( `is` ))


分隔符 # 未找到

print (string.partition ( `bad` ))

string = "geeks 是個好東西,不是嗎"


# 拆分時 &# 39;是 '

print (string.partition ( `是` ))

輸出:

(`geeks `, `is`, `a good`) (`geeks is a good`, ` `,` `) (`geeks`,` is`, "a good, not it") 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method