python - 在python中使用for循环打印几个字符串
问题描述
我正在尝试在 Python 3.7 的 print 语句中申请循环。
例如
string1="Liverpool is always alone"
string2="Manchester United is the best team in the world"
string3="Tottenham Hotspur is for losers"
string4="Leicester City is overrated"
for i in range(1,5):
print(string%i.find(" is")) # <---this is the problem
我的最终目标是获得
9
17
17
14
当然,我可以将结果存储在列表中,然后像这样打印结果:
results=[string1.find(" is"),
string2.find(" is"),
string3.find(" is"),
string4.find(" is")]
for i in range(1,4):
print(results[i])
但是特别是当字符串的数量变得太多时会很麻烦。
请建议一种使用 for 循环打印多个字符串的方法。
我正在使用 Python 3.7。
解决方案
将字符串放入列表中:
statements = [
"Liverpool is always alone",
"Manchester United is the best team in the world",
"Tottenham Hotspur is for losers",
"Leicester City is overrated",
]
然后您可以轻松地遍历它们:
for s in statements:
print(s.find(" is"))
推荐阅读
- javascript - 将 Promise.race 与数据数组结合作为参数
- swift - 从字符串创建类类型以作为具体类型传递给泛型参数 Swift
- hibernate - 无法使用 STR_TO_DATE 将字符串日期转换为休眠日期(在休眠中)
- python - 在nsga3中生成参考点
- airflow - 从另一个项目访问存储桶?
- r - 将 stlm 模型重新拟合到整个训练集时出错
- apache-kafka - 为KAFKA搭建多broker集群,并试图杀死Leader Broker
- php - 供应商文件夹中的 CodeIgniter 控制器
- spring - 当我们在spring数据中删除或添加子实体时,数据版本不会增加?
- java - 如何在 Android 测试中创建模拟服务?