単語の先頭文字を大文字にする

PythonPythonBeginner
今すぐ練習

This tutorial is from open-source community. Access the source code

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

Pythonでは、組み込み関数を使って文字列の各単語の先頭文字を大文字にすることができます。このチャレンジでは、文字列引数を受け取り、各単語の先頭文字を大文字にした新しい文字列を返す関数を書くよう求められます。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/BasicConceptsGroup -.-> python/comments("Comments") python/FunctionsGroup -.-> python/function_definition("Function Definition") subgraph Lab Skills python/comments -.-> lab-13595{{"単語の先頭文字を大文字にする"}} python/function_definition -.-> lab-13595{{"単語の先頭文字を大文字にする"}} end

各単語の先頭文字を大文字にする

文字列sを引数として受け取り、各単語の先頭文字を大文字にした新しい文字列を返す関数capitalize_every_word(s)を書きましょう。

def capitalize_every_word(s):
  return s.title()
capitalize_every_word('hello world!') ## 'Hello World!'

まとめ

このチャレンジでは、Pythonの組み込み関数を使って文字列の各単語の先頭文字を大文字にする方法を学びました。このスキルは、表示用にテキストを整形したり、ユーザー入力を処理したりするなど、さまざまなアプリケーションで役立つことができます。