📜  Python字符串 rsplit() 方法

📅  最后修改于: 2022-05-13 01:55:24.227000             🧑  作者: Mango

Python字符串 rsplit() 方法

Python String rsplit()方法在用指定的分隔字符串字符串返回字符串列表。

示例 1

Python3
# Python code to split a string
# using rsplit.
  
# Splits at space
word = 'geeks for geeks'
print(word.rsplit())
  
# Splits at 'g'. Note that we have
# provided maximum limit as 1. So
# from right, one splitting happens
# and we get "eeks" and "geeks, for, "
word = 'geeks, for, geeks'
print(word.rsplit('g', 1))
  
# Splitting at '@' with maximum splitting
# as 1
word = 'geeks@for@geeks'
print(word.rsplit('@', 1))


Python3
word = 'geeks, for, geeks, pawan'
  
# maxsplit: 0
print(word.rsplit(', ', 0))
  
# maxsplit: 4
print(word.rsplit(', ', 4))
  
word = 'geeks@for@geeks@for@geeks'
  
# maxsplit: 1
print(word.rsplit('@', 1))
  
# maxsplit: 2
print(word.rsplit('@', 2))


Python3
word = 'geeks for geeks'
  
# Since separator is 'None', 
# so, will be splitted at space
print(word.rsplit(None, 1))
  
print(word.rsplit(None, 2))
  
# Also observe these
print('@@@@@geeks@for@geeks'.rsplit('@'))
print('@@@@@geeks@for@geeks'.rsplit('@', 1))
print('@@@@@geeks@for@geeks'.rsplit('@', 3))
print('@@@@@geeks@for@geeks'.rsplit('@', 5))


输出:

['geeks', 'for', 'geeks']
['geeks, for, ', 'eeks']
['geeks@for', 'geeks']

示例 2

Python3

word = 'geeks, for, geeks, pawan'
  
# maxsplit: 0
print(word.rsplit(', ', 0))
  
# maxsplit: 4
print(word.rsplit(', ', 4))
  
word = 'geeks@for@geeks@for@geeks'
  
# maxsplit: 1
print(word.rsplit('@', 1))
  
# maxsplit: 2
print(word.rsplit('@', 2))

输出:

['geeks, for, geeks, pawan']
['geeks', 'for', 'geeks', 'pawan']
['geeks@for@geeks@for', 'geeks']
['geeks@for@geeks', 'for', 'geeks']

示例 3

Python3

word = 'geeks for geeks'
  
# Since separator is 'None', 
# so, will be splitted at space
print(word.rsplit(None, 1))
  
print(word.rsplit(None, 2))
  
# Also observe these
print('@@@@@geeks@for@geeks'.rsplit('@'))
print('@@@@@geeks@for@geeks'.rsplit('@', 1))
print('@@@@@geeks@for@geeks'.rsplit('@', 3))
print('@@@@@geeks@for@geeks'.rsplit('@', 5))

输出:

['geeks for', 'geeks']
['geeks', 'for', 'geeks']
['', '', '', '', '', 'geeks', 'for', 'geeks']
['@@@@@geeks@for', 'geeks']
['@@@@', 'geeks', 'for', 'geeks']
['@@', '', '', 'geeks', 'for', 'geeks']