python - 这个 Python 字符串中的 {0} 是什么意思?

标签 python python-3.x

以下程序在字符串中使用 {0},我不确定它是如何工作的,它出现在有关 Python 迭代的在线教程中,我似乎找不到任何解释它的地方。

import random

number = random.randint(1, 1000)
guesses = 0

print("I'm thinking of a number between 1 and 1000.")

while True:
   guess = int(input("\nWhat do you think it is? "))
   guesses += 1

    if guess > number:
        print("{0} is too high.".format(guess))
    elif guess < number: 
        print("{0} is too low.".format(guess))
    else:
        break

print("\nCongratulations, you got it in {0} guesses!\n".format(guesses))

谢谢!

最佳答案

这是格式方法的一个指标,您希望它被格式的第一个(索引零)参数替换。 (例如 "2 + 2 = {0}".format(4))

关于python - 这个 Python 字符串中的 {0} 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6682806/

相关文章:

python - Django 迭代 - 计算总和

python - 这个 List Overlap 程序给出了奇怪的输出

python - 来自不同虚拟 IP 的 1024 个连接后超时

python - 如何从 __init__.py 导入应用程序以及为什么会收到此错误?

python-3.x - 如何摆脱 Python Pandas 中的斜体并获取纯文本?

python - 列表中的所有 6 数排列

algorithm - 提高以下算法的效率

python - 有没有办法可以为列表中的同一个团队加分?

python - 在互斥参数组中使用 stdin

python - 为时间序列问题寻找 acf 值的问题