Python | レベル別練習問題:文字列メソッド

Python
スポンサーリンク

「文字列メソッド」練習問題50問+解答+解説付き(初心者〜中級者向け)
を紹介します。


  • 難易度を「基礎 → 中級 → 応用」と3段階に分けています。
  • 各問題は短く実行できる形で書いてあるので、コピー&実行で確認できます!

  1. 【基礎編】(Q1〜Q15)
    1. Q1. 大文字に変換
    2. Q2. 小文字に変換
    3. Q3. 前後の空白削除
    4. Q4. 左側の空白だけ削除
    5. Q5. 右側の空白だけ削除
    6. Q6. 部分文字列の置き換え
    7. Q7. 一部だけ置き換え(回数指定)
    8. Q8. 区切りで分割
    9. Q9. リストを文字列に連結
    10. Q10. 部分文字列を検索
    11. Q11. 見つからない場合の挙動
    12. Q12. 指定位置以降を検索
    13. Q13. 文字数を数える
    14. Q14. 文字列が特定の語で始まるか
    15. Q15. 文字列が特定の語で終わるか
  2. 【中級編】(Q16〜Q35)
    1. Q16. 小文字変換 → 検索
    2. Q17. replace() と upper() の組み合わせ
    3. Q18. strip() の応用(改行削除)
    4. Q19. split() → join() の連携
    5. Q20. 文字列の一部を置き換え(数字を含む)
    6. Q21. 空文字列で join()
    7. Q22. 複数の置き換えを連続で行う
    8. Q23. find() の結果を条件分岐に使う
    9. Q24. startswith() を複数条件で
    10. Q25. replace() で空白削除(別の使い方)
    11. Q26. 複数スペースを1つにまとめる
    12. Q27. splitlines()(改行で分割)
    13. Q28. 大文字・小文字の変換確認
    14. Q29. すべて大文字かチェック
    15. Q30. 英数字かどうか判定
    16. Q31. 数字だけか判定
    17. Q32. join() の応用:改行で結合
    18. Q33. find() + スライス
    19. Q34. strip() で指定文字を削除
    20. Q35. replace() で改行除去
  3. 【応用編】(Q36〜Q50)
    1. Q36. メールのドメイン抽出
    2. Q37. 拡張子を取得
    3. Q38. URL からドメインだけ取り出す
    4. Q39. ファイル名部分だけ取り出す
    5. Q40. 文字数カウント(スペース除く)
    6. Q41. 文字列を中央寄せ
    7. Q42. 左寄せ・右寄せ
    8. Q43. カウントメソッド
    9. Q44. capitalize()(先頭を大文字)
    10. Q45. title()(単語ごとに大文字)
    11. Q46. swapcase()(大文字⇄小文字反転)
    12. Q47. replace()で改行→カンマ区切り
    13. Q48. f文字列とメソッド併用
    14. Q49. startswith + endswith 応用
    15. Q50. チェーン操作(複数メソッド連続)

【基礎編】(Q1〜Q15)

文字列メソッドの基本操作に慣れよう。


Q1. 大文字に変換

word = "hello"
print(word.upper())
Python

出力結果は?

答え: HELLO
🧩 解説: .upper() は英字をすべて大文字にします。


Q2. 小文字に変換

word = "PYTHON"
print(word.lower())
Python

答え: python


Q3. 前後の空白削除

text = "  apple  "
print(text.strip())
Python

答え: apple
🧩 .strip() は前後の空白を除去します。


Q4. 左側の空白だけ削除

text = "   hi!"
print(text.lstrip())
Python

答え: "hi!"


Q5. 右側の空白だけ削除

text = "hi!   "
print(text.rstrip())
Python

答え: "hi!"


Q6. 部分文字列の置き換え

text = "I love cats"
print(text.replace("cats", "dogs"))
Python

答え: "I love dogs"


Q7. 一部だけ置き換え(回数指定)

text = "apple apple apple"
print(text.replace("apple", "orange", 2))
Python

答え: "orange orange apple"


Q8. 区切りで分割

data = "red,blue,green"
print(data.split(","))
Python

答え: ['red', 'blue', 'green']


Q9. リストを文字列に連結

colors = ["red", "blue", "green"]
print("-".join(colors))
Python

答え: "red-blue-green"


Q10. 部分文字列を検索

s = "apple banana orange"
print(s.find("banana"))
Python

答え: 6


Q11. 見つからない場合の挙動

s = "apple banana"
print(s.find("grape"))
Python

答え: -1


Q12. 指定位置以降を検索

s = "banana banana"
print(s.find("banana", 3))
Python

答え: 7


Q13. 文字数を数える

text = "Python3"
print(len(text))
Python

答え: 7


Q14. 文字列が特定の語で始まるか

filename = "data.csv"
print(filename.startswith("data"))
Python

答え: True


Q15. 文字列が特定の語で終わるか

filename = "report.pdf"
print(filename.endswith(".csv"))
Python

答え: False


【中級編】(Q16〜Q35)

応用的な使い方や複数メソッドの組み合わせに挑戦!


Q16. 小文字変換 → 検索

s = "HELLO Python"
print(s.lower().find("python"))
Python

答え: 6


Q17. replace() と upper() の組み合わせ

msg = "good morning"
print(msg.replace("morning", "evening").upper())
Python

答え: "GOOD EVENING"


Q18. strip() の応用(改行削除)

line = "Hello\n"
print(line.strip())
Python

答え: "Hello"


Q19. split() → join() の連携

text = "a b c"
print(",".join(text.split()))
Python

答え: "a,b,c"


Q20. 文字列の一部を置き換え(数字を含む)

msg = "ver1.0"
print(msg.replace("1.0", "2.0"))
Python

答え: "ver2.0"


Q21. 空文字列で join()

letters = ["P", "y", "t", "h", "o", "n"]
print("".join(letters))
Python

答え: "Python"


Q22. 複数の置き換えを連続で行う

text = "Hello World!"
text = text.replace("Hello", "Hi").replace("World", "Python")
print(text)
Python

答え: "Hi Python!"


Q23. find() の結果を条件分岐に使う

email = "user@example.com"
if email.find("@") != -1:
    print("OK")
else:
    print("NG")
Python

答え: OK


Q24. startswith() を複数条件で

file = "report.docx"
print(file.startswith(("report", "data")))
Python

答え: True


Q25. replace() で空白削除(別の使い方)

text = "a b c"
print(text.replace(" ", ""))
Python

答え: "abc"


Q26. 複数スペースを1つにまとめる

text = "I   like   Python"
print(" ".join(text.split()))
Python

答え: "I like Python"


Q27. splitlines()(改行で分割)

data = "apple\nbanana\norange"
print(data.splitlines())
Python

答え: ['apple', 'banana', 'orange']


Q28. 大文字・小文字の変換確認

s = "Python"
print(s.islower(), s.isupper())
Python

答え: False False


Q29. すべて大文字かチェック

s = "HELLO"
print(s.isupper())
Python

答え: True


Q30. 英数字かどうか判定

s = "abc123"
print(s.isalnum())
Python

答え: True


Q31. 数字だけか判定

num = "12345"
print(num.isdigit())
Python

答え: True


Q32. join() の応用:改行で結合

lines = ["line1", "line2", "line3"]
print("\n".join(lines))
Python

答え:

line1
line2
line3

Q33. find() + スライス

s = "abc=123"
pos = s.find("=")
print(s[pos+1:])
Python

答え: "123"


Q34. strip() で指定文字を削除

s = "###Python###"
print(s.strip("#"))
Python

答え: "Python"


Q35. replace() で改行除去

text = "Hello\nWorld\n"
print(text.replace("\n", " "))
Python

答え: "Hello World "


【応用編】(Q36〜Q50)

実践で役立つ文字列処理を練習!


Q36. メールのドメイン抽出

email = "user@example.com"
print(email.split("@")[1])
Python

答え: "example.com"


Q37. 拡張子を取得

file = "data.csv"
print(file.split(".")[-1])
Python

答え: "csv"


Q38. URL からドメインだけ取り出す

url = "https://www.python.jp/train/"
print(url.split("/")[2])
Python

答え: "www.python.jp"


Q39. ファイル名部分だけ取り出す

path = "C:/Users/Halu/report.txt"
print(path.split("/")[-1])
Python

答え: "report.txt"


Q40. 文字数カウント(スペース除く)

s = "I love Python"
print(len(s.replace(" ", "")))
Python

答え: 11


Q41. 文字列を中央寄せ

print("Python".center(10, "-"))
Python

答え: "--Python--"


Q42. 左寄せ・右寄せ

print("A".ljust(5, "*"))
print("A".rjust(5, "*"))
Python

答え:

A****
****A

Q43. カウントメソッド

s = "banana"
print(s.count("a"))
Python

答え: 3


Q44. capitalize()(先頭を大文字)

s = "python programming"
print(s.capitalize())
Python

答え: "Python programming"


Q45. title()(単語ごとに大文字)

s = "hello world from python"
print(s.title())
Python

答え: "Hello World From Python"


Q46. swapcase()(大文字⇄小文字反転)

s = "Hello"
print(s.swapcase())
Python

答え: "hELLO"


Q47. replace()で改行→カンマ区切り

s = "A\nB\nC"
print(s.replace("\n", ","))
Python

答え: "A,B,C"


Q48. f文字列とメソッド併用

name = "halu"
print(f"Hello, {name.upper()}!")
Python

答え: "Hello, HALU!"


Q49. startswith + endswith 応用

url = "https://python.jp"
print(url.startswith("https") and url.endswith(".jp"))
Python

答え: True


Q50. チェーン操作(複数メソッド連続)

text = "  hello world  "
print(text.strip().replace("world", "Python").upper())
Python

答え: "HELLO PYTHON"


Pythonの「文字列」は非常に多機能で、メソッドを組み合わせることで
実務レベルのテキスト処理も簡単に書けるようになります。

Python
スポンサーリンク
シェアする
@lifehackerをフォローする
スポンサーリンク
タイトルとURLをコピーしました