文字列

Python のデータ型

Jason Myers

Instructor

フォーマット済み文字列の作成

  • f文字列(フォーマット済み文字列リテラル)- f""
cookie_name = "Anzac"
cookie_price = "$1.99"

print(f"Each { cookie_name } cookie costs { cookie_price }.")
"Each Anzac cookie costs $1.99."
Python のデータ型

文字列による結合

  • "".join() は、呼び出した文字列を区切り文字としてイテラブルを結合します
child_ages = ["3", "4", "7", "8"]

print(", ".join(child_ages))
"3, 4, 7, 8"
print(f"The children are ages {','.join(child_ages[0:3])}, and {child_ages[-1]}.")
"The children are ages 3, 4, 7, and 8."
Python のデータ型

文字列の部分一致

  • .startswith().endswith() は、文字列が指定の文字または文字列で始まる・終わるかを判定するメソッドです
boy_names = ["Mohamed", "Youssef", "Ahmed"]
print([name for name in boy_names if name.startswith('A')])
["Ahmed"]
  • これらのメソッドを含む多くの文字列関数は大文字・小文字を区別することに注意してください。
Python のデータ型

文字列内の検索

  • in 演算子は、文字列などのイテラブル型の中から指定の値を検索します
"long" in "Life is a long lesson in humility."
True
"life" in "Life is a long lesson in humility."
False
Python のデータ型

大文字・小文字を区別しない方法

  • .lower() メソッドは文字列を小文字に変換して返します
"life" in "Life is a long lesson in humility.".lower()
True
Python のデータ型

練習しましょう!

Python のデータ型

Preparing Video For Download...