Python字符串长度递归

标签 python string recursion string-length

我正在尝试编写一个简单的函数来递归地计算字符串长度。

我可以轻松地进行求和、斐波那契和阶乘运算,但我正在尝试创建只有一个参数的最简单的函数,我不喜欢将第二个参数用作计数器索引..

可以给我发一些小东西吗?

最佳答案

这是您要找的吗?

def recursiveLength(theString):
    if theString == '': return 0
    return 1 + recursiveLength(theString[1:])

关于Python字符串长度递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5670060/

相关文章:

python - 大于 MTU 的数据包到达 TUN 接口(interface)

python - .split python 字数统计

sql - 递归查询在服务器上执行但不在本地执行

c++ - 不可变字符串与 std::string

javascript - JS : need a css color code based on a string

javascript - 如何递归地对数字求和javascript

c++ - 计算图中路径的递归函数的复杂性

python - 查找csv文件中的列数

python - 用正则表达式构建字典

python - 笨拙的 lambda 函数