movie_title = "A Whole New World"
# 指定した文字が出現する回数を返す
print(movie_title.count('W'), movie_title.count('A'))
# => 2 1
# 文字列中に指定した文字がある場合に、該当する最小のインデックスを返す
# ただし、文字列が見つからない場合は、-1を返す
print(movie_title.find('N'), movie_title.find('W'), movie_title.find('X'))
# => 8 2 -1
# 文字列中に指定した文字がある場合に、該当する最大のインデックスを返す
# ただし、文字列が見つからない場合は、-1を返す
print(movie_title.rfind('N'), movie_title.rfind('W'), movie_title.rfind('X'))
# => 8 12 -1
# 先頭の文字列が、指定した文字列で始まるか否かを真偽値で返す
print(movie_title.startswith('A'), movie_title.startswith('X'))
# => True False
# 先頭の文字列が、指定した文字列で終わるか否かを真偽値で返す
print(movie_title.endswith('A'), movie_title.endswith('X'))
# => False False
# 文字列中に指定した文字がある場合、該当する最小のインデックスを返す
# ただし、文字列が見つからない場合は、例外を発生させる
print(movie_title.index('N'), movie_title.index('W'), movie_title.index('X'))
# => ValueError: substring not found
# 文字列中に指定した文字がある場合、該当する最大のインデックスを返す
# ただし、文字列が見つからない場合は、例外を発生させる
print(movie_title.rindex('N'), movie_title.rindex('W'), movie_title.rindex('X'))
# => ValueError: substring not found
# 指定した文字列が、検索対象の文字列に含まれているか否かを調べる
print('World' in movie_title,"Sekai" in movie_title)
# => True False
以上です。
0 件のコメント:
コメントを投稿