如何在不使用变量的情况下找到以下字符串的长度?
string = "This is a sentence and it has %d characters" % len(string)
print string
这个不行。正确的编码方式是什么?
最佳答案
在你的句子中,你试图在分配任何值之前评估字符串长度
string = "This is a sentence and it has %d characters" % len(string)
您应该考虑在哪里可以做到这一点,即:
string = "%d chars.."
可以同样扩展为
string = "9 chars.."
致:
string = "10 chars.."
关于python - 在字符串中使用 len(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21982063/