まぬねこの足跡。。。

備忘録+たのしさ+ひっそりと

Python - str.rsplit(sep=None, maxsplit=- 1):後ろから、文字列分割

概要

str.rsplit(sep=None, maxsplit=- 1)

str:区切り文字を含んだ文字列
sep:区切り文字。省略時、空白
maxsplit:最大区切り数

  • 後ろから、文字列分割
  • 組込み型 文字列メソッド

実行結果

print("123,45,678,9".rsplit(","))
print("123,45,678,9".rsplit(",",2))
print("123,45,678,9".rsplit(",",maxsplit=2))
print("123,45,678,9".rsplit(" "))

表示イメージ

['123', '45', '678', '9']
['123,45', '678', '9']
['123,45', '678', '9']
['123,45,678,9']

Python - str.rpartition(sep):文字列 最後の位置で2分割+区切り文字

概要

str.rpartition(sep)

str:区切り文字を含んだ文字列
sep:区切り文字

  • 最後の区切り文字で区切った2分割文字列+区切り文字
  • 区切れない時、2つの空文字列とそのまま文字列
  • 組込み型 文字列メソッド

実行結果

print("123,45,6789".rpartition(","))
print("123,45,6789".rpartition(" "))

表示イメージ

('123,45', ',', '6789')
('', '', '123,45,6789')

Python - str.rjust(width[, fillchar]):長さ指定+右寄せ

概要

str.rjust(width[, fillchar])

str:右寄せ文字列
width:文字長さ
fillchar::空埋め文字 ※省略時、半角空白

  • 長さ指定+右寄せ
  • 組込み型 文字列メソッド

実行結果

print("abcde".rjust(10))
print("abcde".rjust(5))
print("abcde".rjust(3))
print("abcde".rjust(10,'#'))
print("abcde".rjust(5,'#'))
print("abcde".rjust(3,'#'))

表示イメージ

     abcde
abcde
abcde
#####abcde
abcde
abcde

Python - str.rindex(sub[, start[, end]]):文字列検索 最大インデックスを返す (ない時、ValueError )

概要

str.rindex(sub[, start[, end]])

str:置換えられる文字列
start,end:スライスと同一

  • 文字列検索 最大インデックスを返す
  • 見つからない時、「ValueError」
  • 組込み型 文字列メソッド

実行結果

print("1234567123456712345671234567".rindex("345"))
print("1234567123456712345671234567".rindex("345",5))
print("1234567123456712345671234567".rindex("345",5,20))

表示イメージ

 23
 23
 16
 -----------------------------------------------------
 ValueError         Traceback (most recent call last)

Python - str.rfind(sub[, start[, end]]):文字列検索 最大インデックスを返す

概要

str.rfind(sub[, start[, end]])

str:置換えられる文字列
start,end:スライスと同一

  • 文字列検索 最大インデックスを返す
  • 見つからない時、「-1」
  • 組込み型 文字列メソッド

実行結果

print("1234567123456712345671234567".rfind("345"))
print("1234567123456712345671234567".rfind("345",5))
print("1234567123456712345671234567".rfind("345",5,20))
print("1234567123456712345671234567".rfind("345",5,10))

表示イメージ

 23
 23
 16
 -1

Python - str.replace(old, new[, count]):文字列 置換え

概要

str.replace(old, new[, count])

str:置換えられる文字列
old:置換前 文字列
new:置換後 文字列
count:先頭から何回目のみ置換えるか

  • 文字列 置換え
  • 組込み型 文字列メソッド

実行結果

print("1234567123456712345671234567".replace("345", "ABC"))
print("1234567123456712345671234567".replace("345", "ABC",2))
print("1234567123456712345671234567".replace("3456", "AB"))
print("1234567123456712345671234567".replace("34", "ABCD"))

表示イメージ

12ABC6712ABC6712ABC6712ABC67
12ABC6712ABC6712345671234567
12AB712AB712AB712AB7
12ABCD56712ABCD56712ABCD56712ABCD567

Python - str.removesuffix(suffix, /):接尾辞 除去

概要

str.removesuffix(suffix, /)

str:削除される文字列
prefix:接頭辞

  • 接尾辞 除去
  • 組込み型 文字列メソッド

実行結果

print("たこさん".removesuffix("さん"))

表示イメージ

たこ