partition()方法在第一次出現分隔符時分割字符串,並返回一個元組,其中包含分隔符之前的部分,分隔符和分隔符之後的部分。這裏的分隔符是一個帶有參數的字符串。
用法:
string.partition(separator)
參數:
partition()方法采用分隔符(字符串)作為參數,用於在首次出現字符串時將其分隔開。
返回:
返回一個元組,其中包含在分隔符,分隔符參數之前的部分以及在字符串中找到分隔符的情況下分隔符之後的部分。
返回一個元組,該元組包含字符串本身和兩個空字符串(如果未找到spacer參數)。
代碼1
string = "pawan is a good"
# 'is' separator is found
print(string.partition('is '))
# 'not' separator is not found
print(string.partition('bad '))
string = "pawan is a good, isn't it"
# splits at first occurrence of 'is'
print(string.partition('is'))
輸出:
('pawan ', 'is ', 'a good') ('pawan is a good', '', '') ('pawan ', 'is', " a good, isn't it")
CODE2
string = "geeks is a good"
# 'is' separator is found
print(string.partition('is '))
# 'not' separator is not found
print(string.partition('bad '))
string = "geeks is a good, isn't it"
# splits at first occurrence of 'is'
print(string.partition('is'))
輸出:
('geeks ', 'is ', 'a good') ('geeks is a good', '', '') ('geeks ', 'is', " a good, isn't it")
相關用法
- Python numpy.partition()用法及代碼示例
- Python Sympy Partition.as_dict()用法及代碼示例
- Python Sympy Partition.RGS_generalized()用法及代碼示例
- Python sympy Partition()用法及代碼示例
- Python Sympy Partition.RGS_enum()用法及代碼示例
- Python Sympy Partition.next_lex()用法及代碼示例
- Python Sympy Partition.prev_lex()用法及代碼示例
- Python Sympy Partition.RGS_unrank()用法及代碼示例
- Python sympy.partition()用法及代碼示例
- Python Pandas Series.str.partition()用法及代碼示例
- Python Sympy Partition.RGS_rank()用法及代碼示例
- Python Sympy Partition.conjugate()用法及代碼示例
- Python Numpy matrix.partition()用法及代碼示例
- Python Sympy Partition.as_ferrers()用法及代碼示例
- Python Numpy recarray.partition()用法及代碼示例
注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 String partition() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。