第11章 文字列操作 / レッスン 2
文字列メソッド一覧
学習時間のめやす:約15分
演習 113OSマルチプラットフォーム対応
文字列操作は、WindowsやMacやLinuxといったOS環境に依存しないテキスト処理を可能にします。
特に、¥やバックスラッシュなどOSによって異なるファイルパスやURLの処理に有効です。
# ファイルパス import os path = os.path.join("home", "user", "documents", "file.txt") print(path) # 出力: home/user/documents/file.txt (Linux)
演習 114capitalize(文字列の先頭文字を大文字にし、他の文字を小文字にします)
message = "hello world" print(message.capitalize()) # Output: Hello world
演習 115casefold(文字列を小文字に変換します(比較に最適)
message = "HELLO WORLD" print(message.casefold()) # Output: hello world
演習 116center(指定幅で文字列を中央揃えにします)
message = "hello" print(message.center(10)) # Output: ' hello '
演習 117count(部分文字列が文字列内に現れる回数を返します)
message = "hello hello hello" print(message.count("hello")) # Output: 3
演習 118encode(文字列をバイト型にエンコードします)
message = "hello" print(message.encode()) # Output: b'hello'
演習 119endswith(文字列が指定した文字列で終わる場合にTrueを返します)
message = "hello world" print(message.endswith("world")) # Output: True
演習 120expandtabs(タブ文字\tを指定したスペース数に変換します)
message = "hello\tworld" print(message.expandtabs(4)) # Output: hello world
演習 121find(部分文字列が最初に見つかった位置を返します(見つからない場合は-1)
message = "hello world" print(message.find("world")) # Output: 6
演習 122format
レースホルダーを指定した値で置き換えます
template = "Hello, {}!" print(template.format("Alice")) # Output: Hello, Alice!
演習 123format_map(dictを使ったプレースホルダー置き換えを行います)
template = "Name: {name}, Age: {age}" data = {"name": "Alice", "age": 25} print(template.format_map(data)) # Output: Name: Alice, Age: 25
演習 124index(部分文字列が最初に見つかった位置を返します(見つからない場合は例外をスロー)
message = "hello world" print(message.index("world")) # Output: 6
演習 125isalnum(文字列が英数字のみで構成されている場合にTrueを返します)
message = "hello123" print(message.isalnum()) # Output: True
演習 126isalpha(文字列がアルファベットのみで構成されている場合にTrueを返します)
message = "hello" print(message.isalpha()) # Output: True
演習 127isascii(文字列がASCII文字のみで構成されている場合にTrueを返します)
message = "hello" print(message.isascii()) # Output: True
演習 128isdigit(文字列が数字のみで構成されている場合にTrueを返します)
message = "12345" print(message.isdigit()) # Output: True
演習 129islower(文字列がすべて小文字で構成されている場合にTrueを返します)
message = "hello" print(message.islower()) # Output: True
演習 130isnumeric(文字列が数値のみで構成されている場合にTrueを返します)
message = "12345" print(message.isnumeric()) # Output: True
演習 131isspace(文字列が空白文字のみで構成されている場合にTrueを返します)
message = " " print(message.isspace()) # Output: True
演習 132istitle(各単語の最初の文字が大文字の場合にTrueを返します)
message = "Hello World" print(message.istitle()) # Output: True
演習 133isupper(文字列がすべて大文字で構成されている場合にTrueを返します)
message = "HELLO" print(message.isupper()) # Output: True
演習 134join(文字列を区切り文字として指定したリストやタプルを結合します)
words = ["hello", "world"] print(" ".join(words)) # Output: hello world
演習 135ljust(左寄せで文字列を指定幅に合わせます)
message = "hello" print(message.ljust(10)) # Output: 'hello '
演習 136lower(文字列を小文字に変換します)
message = "HELLO" print(message.lower()) # Output: hello
演習 137lstrip(文字列の左端の空白を削除します)
message = " hello" print(message.lstrip()) # Output: hello
演習 138partition(指定した文字で文字列を3つに分割します)
message = "hello,world" print(message.partition(",")) # Output: ('hello', ',', 'world')
演習 139replace(部分文字列を別の文字列に置き換えます)
message = "hello world" print(message.replace("world", "Python")) # Output: hello Python
演習 140split(指定した区切り文字で文字列を分割します)
message = "a,b,c" print(message.split(",")) # Output: ['a', 'b', 'c']
演習 141title(各単語の最初の文字を大文字に変換します)
message = "hello world" print(message.title()) # Output: Hello World
