Capitalize Words in a String

  • Share this:

Code introduction


This function takes a string as an argument and returns a new string with the first letter of each word capitalized.


Technology Stack : String, split, list comprehension, capitalize() method

Code Type : String processing

Code Difficulty : Intermediate


                
                    
def capitalize_words(text):
    """
    将输入的字符串中的每个单词的首字母大写。

    :param text: 要转换的字符串
    :return: 首字母大写的字符串
    """
    return ' '.join(word.capitalize() for word in text.split())