Python文字列rsplit()メソッド
例
区切り文字としてスペース(、)を続けてコンマを使用して、文字列をリストに分割します。
txt = "apple, banana, cherry"
x = txt.rsplit(", ")
print(x)
定義と使用法
このrsplit()
メソッドは、文字列を右からリストに分割します。
「max」が指定されていない場合、このメソッドはメソッドと同じものを返し
split()
ます。
注: maxsplitが指定されている場合、リストには指定された数の要素と1が含まれます。
構文
string.rsplit(separator, maxsplit)
パラメータ値
Parameter | Description |
---|---|
separator | Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator |
maxsplit | Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences" |
その他の例
例
文字列を最大2つの項目を持つリストに分割します。
txt = "apple, banana, cherry"
# setting the maxsplit parameter
to 1, will return a list with 2 elements!
x = txt.rsplit(", ", 1)
print(x)