【问题标题】:How can I split and give uppercase to a string at the same time?如何同时拆分字符串并将大写字母赋予字符串?
【发布时间】:2023-02-16 22:01:48
【问题描述】:

我有这个字符串,我对 python 还很陌生,我只需要帮助就可以了。

def this_sentence(我的朋友去旅游了)

我希望输出看起来像这样 ['我的', '朋友', '去', 'ON', 'A', 'TOUR']

【问题讨论】:

    标签: python string split uppercase


    【解决方案1】:

    您甚至不需要单独的函数,只需使用split()upper()

    split_sentence = [word.upper() for word in sentence.split()]
    

    【讨论】:

      【解决方案2】:

      您可以使用 upper() 将所有字符设为大写,然后使用 split() 在每个空格处拆分单词。

      def this_sentence(句子): 返回 sentence.upper().split()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-07
        • 2011-05-28
        • 1970-01-01
        • 2013-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多