[]を使うと文字列の中の任意の文字を取り出せます。[]の中にはゼロ(0)から始まるインデックス番号を指定します。文字列の末尾(右端)から数える場合は、-1から始めます。
図

[0回]
PR
オブジェクト指向とは、プログラミングの機能をグループ化し、これを組み合わせてプログラムを作成していく考え方です。機能単位の独立性が高まり、再利用性がを高めることができます。
[0回]
# 単語の出現回数をカウント
text = """
Keep on asking, and it will be given you:
Keep on seeking, you will find;
Keep on knocking, and it will be opend to you;
for everyone asking receives, and everyone seeking finds,
and to everyone knocking, it will be opend.
"""
# 単語を区切る
text = text.replace(";","") # ;を削除
text = text.replace(",","") # ,を削除
text = text.replace(".","") # .を削除
words = text.split() # 空白で区切ってリスト型を作成
# 単語を数える
counter = {} # counterという空の辞書型を作成
for w in words:
ws = w.lower() # 小文字に変換
if ws in counter: # もし辞書型にすでにキーがあれば値を1つ追加
counter[ws] += 1
else:
counter[ws] = 1 # もし辞書型にキーがなければ、値を1としてキーも登録
# 結果を表示。3回以上のものだけを表示
for k,v in sorted(counter.items()): # counterのキーをアルファベット順として範囲に指定
if v >= 3:
print(k, v)
[0回]