【问题标题】:Count letters in string python [duplicate]计算字符串python中的字母[重复]
【发布时间】:2016-01-01 13:20:09
【问题描述】:

(python新手和堆栈溢出)

我很好奇是否有一种方法可以计算 python 字符串中字母的数量。例如:

string="hello"

我只是想要一些东西来计算字母然后将其输出到一个变量中以供以后使用。

【问题讨论】:

    标签: python windows


    【解决方案1】:

    以下将给出字符串的长度:

    len(string)
    

    在你的情况下,你可以分配它:

    numLetters = len(string)
    

    此函数可用于字符串以外的其他对象。如需其他用途,请阅读documentation

    【讨论】:

      【解决方案2】:

      使用python函数len,即:

      size = len(string)
      

      len()
      

      https://docs.python.org/2/library/functions.html#len


      演示

      https://ideone.com/mhpdLi

      【讨论】:

        猜你喜欢
        • 2017-04-20
        • 2014-07-17
        • 2016-09-03
        • 1970-01-01
        • 2012-01-11
        • 1970-01-01
        • 2021-08-04
        • 2014-11-01
        相关资源
        最近更新 更多