Pythonのendswith関数の使用方法を解説します。
endswith()関数は、文字列が指定されたサフィックスで終わるかどうかを判別するために使用されます。その使い方は次のとおりです:
string.endswith(suffix[, start[, end]])
文字列stringは検査対象の文字列、suffixは検査対象の接尾辞です。startとendはオプションのパラメータで、検査する文字列の開始位置と終了位置を指定します。
次に、使用例を示します。
string = "Hello, World!"
# 检查字符串是否以"World!"结尾
result = string.endswith("World!")
print(result) # 输出:True
# 检查字符串是否以"Hello"开头
result = string.endswith("Hello")
print(result) # 输出:False
# 检查字符串中从索引2开始的子字符串是否以"World!"结尾
result = string.endswith("World!", 2)
print(result) # 输出:False
# 检查字符串中从索引2到索引13的子字符串是否以"World!"结尾
result = string.endswith("World!", 2, 13)
print(result) # 输出:True
endswith 関数は大文字と小文字を区別することに注意してください。大文字と小文字を無視するには、文字列を先に大文字か小文字に変換してから、判断を行うことができます。