【问题标题】:Analyzing strings in Python [closed]在 Python 中分析字符串 [关闭]
【发布时间】:2019-01-24 04:41:37
【问题描述】:

我想用 python 编写一个程序来分析用户输入。

例如,如果用户写“你好,我 19 岁”,程序可以看到 字符串中包含数字并将“19”分配给变量。

还可以同时看到字符串中有“years”这个词,并生成变量:

years = 19

我对 python 和编程很陌生,如果问题很奇怪,请见谅。

谢谢。

【问题讨论】:

标签: python string input


【解决方案1】:

您可以使用函数len 计算字符串的长度。 str 可以包含所有字符和字母。试试下面的代码:

word = input("Waiting for input: ")
print("Found "+str(len(word))+" letters!")

测试它:

Waiting for input: hello
Found 5 letters!

用数字和其他字符测试它:

Waiting for input: hello123!@#
Found 11 letters!

【讨论】:

  • 这很方便,但我希望它也能够分析输入字符串中的不同字符和数字..有点难以解释
  • @kanuki 你看过编辑吗?它也适用于不同的字符和数字。
猜你喜欢
  • 2016-02-02
  • 2019-05-27
  • 2014-07-10
  • 2013-05-12
  • 2014-09-14
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多