【问题标题】:Assistance with Simple Strings协助简单字符串
【发布时间】:2021-02-25 10:30:29
【问题描述】:

这里是初学者。

在如何计算字符串的每个单词中的字符时需要一些帮助吗?

看起来很简单,但显然我错过了一些东西。

谢谢,SWMH

【问题讨论】:

  • 请提供更多细节。
  • 请直接在此处发布您的实际问题。对如何提问有疑问?检查this article
  • @AmitKumar 假设我输入了文本“你好,早上好”,我希望 Python 输出 Hello = 5 Good = 4 Morning = 7 所以计算每个单词中的字符数
  • @SW96808 按空格分割字符串,然后遍历每个单词,然后调用 len。
  • 当您创建一个新问题时,请在问题标题上更加客观。例如,一个好的问题标题是How to discover the length of a string in Python?

标签: python string oop count character


【解决方案1】:

根据您的评论,您可以执行以下操作:

def return_length():
    
    words = input("Please input words\n")
    word_list = words.split(" ")  # Splits the sentence into list by spaces
    for words in word_list:  # loops over the list and prints the length
        print("String: \"{}\" is \"{}\" characters long".format(words, len(words)))


if __name__ == "__main__":
    return_length()

【讨论】:

    【解决方案2】:

    您应该使用len 函数。

    str = "Hello World"
    str_length = len(str)
    

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多