まぬねこの足跡。。。

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

Python3

Python 文字列メソッド

文字列メソッド 変換 文字加工 埋める 寄せる フォーマット 文字結合 トリミング 分割 置換え 状態 判定 検索 文字列メソッド 文字列.メソッド名(引数, ....) の形式の関数のようなもの 変換 Python - str.capitalize():頭文字のみ大文字 変換 Python - str…

Python - str.zfill(width):長さ指定+0埋め

概要 実行結果 表示イメージ 概要str.zfill(width)str:文字列 width:文字長さ。(符号の桁を含む) 長さ指定+0埋め 先頭が符号だった時、符号の後から0埋めする 組込み型 文字列メソッド 実行結果 print("abcde".zfill(10)) print("-abcde".zfill(10)) 表…

Python - str.upper():全大文字に変換

概要 実行結果 表示イメージ 概要str.upper() 全大文字(大小区別あり)に変換 組込み型 文字列メソッド 実行結果 print("abc123defg".upper()) 表示イメージ ABC123DEFG

Python - str.title():単語毎に、頭文字のみ大文字にする

概要 実行結果 表示イメージ 概要str.title()str:文字列 単語毎に、頭文字のみ大文字にする 組込み型 文字列メソッド 実行結果 print('abc DEF GhI jKL'.title()) 表示イメージ Abc Def Ghi Jkl

Python - str.swapcase():大文字部分⇄小文字部分 変換

概要 実行結果 表示イメージ 概要str.swapcase()str:変換文字列 大文字部分⇄小文字部分 変換 組込み型 文字列メソッド 実行結果 print('abcdABCD123あいう'.swapcase()) 表示イメージ ABCDabcd123あいう

Python - str.strip([chars]):文頭、文末 除去

概要 実行結果 表示イメージ 概要str.strip([chars])str:除去される文字列 chars:除去する文字列。省略時、空白。 文頭、文末 除去 組込み型 文字列メソッド 実行結果 print(" abcde " .strip()) print(" abcde ".strip()) print("####abcde####".strip('#…

Python - str.startswith(prefix[, start[, end]]):文頭が指定文字列か

概要 実行結果 表示イメージ 概要str.startswith(prefix[, start[, end]])str:検索される文字列 prefix:検索文字列 start,end:スライスと同一 文頭が指定文字列か 組込み型 文字列メソッド 実行結果 print("1234567123456712345671234567".startswith("12…

Python - str.splitlines(keepends=False):改行で分割+リスト化

概要 実行結果 表示イメージ 概要str.splitlines(keepends=False)str:改行を含んだ文字列 keepends:Trueでリストに改行を含む 改行で分割+リスト化 組込み型 文字列メソッド 実行結果 print('11 1\n\n22 22\r33\r\n'.splitlines()) print('11 1\n\n22 22\…

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

概要 実行結果 表示イメージ 概要str.split(sep=None, maxsplit=- 1)str:区切り文字を含んだ文字列 sep:区切り文字。省略時、空白 maxsplit:最大区切り数 前から、文字列分割 組込み型 文字列メソッド 実行結果 print("123,45,678,9".split(",")) print("…

Python - str.rstrip([chars]):文末 除去

概要 実行結果 表示イメージ 概要str.rstrip([chars])str:除去される文字列 chars:除去する文字列。省略時、空白。 文末 除去 組込み型 文字列メソッド 実行結果 print(" abcde " .rstrip()) print(" abcde ".rstrip()) print("####abcde####".rstrip('#')…

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

概要 実行結果 表示イメージ 概要str.rsplit(sep=None, maxsplit=- 1)str:区切り文字を含んだ文字列 sep:区切り文字。省略時、空白 maxsplit:最大区切り数 後ろから、文字列分割 組込み型 文字列メソッド 実行結果 print("123,45,678,9".rsplit(",")) pri…

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

概要 実行結果 表示イメージ 概要str.rpartition(sep)str:区切り文字を含んだ文字列 sep:区切り文字 最後の区切り文字で区切った2分割文字列+区切り文字 区切れない時、2つの空文字列とそのまま文字列 組込み型 文字列メソッド 実行結果 print("123,45,678…

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

概要 実行結果 表示イメージ 概要str.rjust(width[, fillchar])str:右寄せ文字列 width:文字長さ fillchar::空埋め文字 ※省略時、半角空白 長さ指定+右寄せ 組込み型 文字列メソッド 実行結果 print("abcde".rjust(10)) print("abcde".rjust(5)) print("…

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

概要 実行結果 表示イメージ 概要str.rindex(sub[, start[, end]])str:置換えられる文字列 start,end:スライスと同一 文字列検索 最大インデックスを返す 見つからない時、「ValueError」 組込み型 文字列メソッド 実行結果 print("1234567123456712345671234567"…

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

概要 実行結果 表示イメージ 概要str.rfind(sub[, start[, end]])str:置換えられる文字列 start,end:スライスと同一 文字列検索 最大インデックスを返す 見つからない時、「-1」 組込み型 文字列メソッド 実行結果 print("1234567123456712345671234567".rfind("3…

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

概要 実行結果 表示イメージ 概要str.replace(old, new[, count])str:置換えられる文字列 old:置換前 文字列 new:置換後 文字列 count:先頭から何回目のみ置換えるか 文字列 置換え 組込み型 文字列メソッド 実行結果 print("12345671234567123456712345…

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

概要 実行結果 表示イメージ 概要str.removesuffix(suffix, /)str:削除される文字列 prefix:接頭辞 接尾辞 除去 組込み型 文字列メソッド 実行結果 print("たこさん".removesuffix("さん")) 表示イメージ たこ

Python - str.removeprefix(prefix, /):接頭辞 削除

概要 実行結果 表示イメージ 概要str.removeprefix(prefix, /)str:削除される文字列 prefix:接頭辞 接頭辞 削除 組込み型 文字列メソッド 実行結果 print("御御御付".removeprefix("御")) 表示イメージ 御御付

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

概要 実行結果 表示イメージ 概要str.partition(sep)str:区切り文字を含んだ文字列 sep:区切り文字 最初の区切り文字で区切った3分割文字列(区切り文字含む) 区切れない時、そのまま文字列と2つの空文字列 組込み型 文字列メソッド 実行結果 print("123,45…

Python - str.partition(sep):文字列 3分割(区切り文字含む)

概要 実行結果 表示イメージ 概要str.partition(sep)str:区切り文字を含んだ文字列 sep:区切り文字 最初の区切り文字で区切った3分割文字列(区切り文字含む) 組込み型 文字列メソッド 実行結果 print("123,45,6789".partition(",")) print("123,45,6789".p…

Python - str.translate(table):変換テーブルで文字列置換え

概要 実行結果 表示イメージ 概要str.translate(table)str:変換する文字列 変換テーブルで文字列置換え Python-str.maketrans():str.translate()用 変換テーブル作成 組込み型 文字列メソッド 実行結果 # ※()内文字表記,()外Unicode # 「97(a)→122(z)」、「65…

Python - str.maketrans(x[, y[, z]]):str.translate()用 変換テーブル作成

概要 実行結果 表示イメージ 概要static str.maketrans(x[, y[, z]])引数[x]:1文字分(Unicode数値、文字)を{変換前:変換後,...}辞書型で紐図ける 引数[x,y]:x,yで同じ長さの文字列、x変換前、y変換後で紐図ける。 引数 z :文字列で指定。文字列とNoneに…

Python - str.lstrip([chars]):先頭 除去

概要 実行結果 表示イメージ 概要str.lstrip([chars])str:除去される文字列 chars:除去する文字列。省略時、空白。 先頭 除去 組込み型 文字列メソッド 実行結果 print(" abcde " .lstrip()) print(" abcde ".lstrip()) print("####abcde####".lstrip('#')…

Python - str.lower():全小文字に変換

概要 実行結果 表示イメージ 概要str.lower() 全小文字(大小区別あり)に変換 組込み型 文字列メソッド 実行結果 print("ABC123abあいうABC".lower()) 表示イメージ abc123abあいうabc

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

概要 実行結果 表示イメージ 概要str.ljust(width[, fillchar])str:左寄せ文字列 width:文字長さ fillchar::空埋め文字 ※省略時、半角空白 長さ指定+左寄せ 組込み型 文字列メソッド 実行結果 print("abcde".ljust(10)) print("abcde".ljust(5)) print("…

Python - str.join(iterable):文字列結合

概要 実行結果 表示イメージ 概要str.join(iterable)str:セパレータになる文字 iterable:結合する文字列 文字列結合 組込み型 文字列メソッド 実行結果 print("~".join(['1','2','3'])) print("~".join(str(x) for x in range(1,4))) # 内包表記でイテラブル出…

Python - str.isupper():1文字以上、全英大文字か

概要 実行結果 表示イメージ 概要str.isupper() 1文字以上、全英大文字か 組込み型 文字列メソッド 実行結果 print("ABC".isupper()) print("abc".isupper()) print("Abc".isupper()) print("AbC".isupper()) 表示イメージ True False False False

Python - str.istitle():文頭のみ英大文字か

概要 実行結果 表示イメージ 概要str.istitle() 文頭が英大文字か 組込み型 文字列メソッド ちょこっとメモタイトルケース文頭のみを英大文字にする 実行結果 print("Abc".istitle()) print("abc".istitle()) print("ABc".istitle()) print("AbcDef".istitle…

Python - str.isspace():1文字以上、全空白文字か

概要 実行結果 表示イメージ 概要str.isspace() 1文字以上、全空白文字か 組込み型 文字列メソッド 実行結果 print("".isprintable()) print(" ".isprintable()) # 全角空白 print(" ".isprintable()) # 半角空白 print("ab c".isprintable()) 表示イメージ …

Python - str.isprintable():全印刷可能文字列、又は空か

概要 実行結果 表示イメージ 概要str.isprintable() 全印刷可能文字列、又は空か 組込み型 文字列メソッド 実行結果 print("あいう".isprintable()) print("".isprintable()) print("\n".isprintable()) 表示イメージ True True False