Python 中输出一行字符数的方法:使用 len() 函数计算字符串长度使用字符串方法 count() 计算空字符的个数(即字符串长度)

如何使用 Python 输出一行中的字符数
在 Python 中,可以使用以下方法输出一行中的字符数:
- 使用 len() 函数:
<code class="python">text = input("请输入一行文本:")
text_length = len(text)
print("字符数:", text_length)</code>- 使用字符串方法 count():
<code class="python">text = input("请输入一行文本:")
num_chars = text.count("")
print("字符数:", num_chars)</code>示例:
<code class="python">text = "Hello World!"
text_length = len(text)
print("字符数:", text_length)</code>输出:
立即学习“Python免费学习笔记(深入)”;
<code>字符数: 12</code>











