String Capitalization Function

  • Share this:

Code introduction


This function takes a string as input and capitalizes the first letter of each word in the string.


Technology Stack : String splitting, list comprehension, string joining

Code Type : String Handling Function

Code Difficulty : Intermediate


                
                    
def capitalize_words(text):
    """
    将输入的字符串中每个单词的首字母大写。
    """
    words = text.split()
    capitalized_words = [word.capitalize() for word in words]
    return ' '.join(capitalized_words)