简体   繁体   English

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

[英]What does {0} mean in this Python string?

The following program uses {0} in a string, and I'm not sure how it works, it came up in an online tutorial about iteration for Python, and I can't seem to find anywhere explaining it.以下程序在字符串中使用 {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))

Thank you!谢谢!

It's an indicator to the format method that you want it to be replaced by the first (index zero) parameter of format.这是格式方法的一个指标,您希望它被格式的第一个(索引零)参数替换。 (eg "2 + 2 = {0}".format(4) ) (例如"2 + 2 = {0}".format(4)

It's a boon for placing same arg multiple times多次放置相同的参数是一个福音

print("When you multiply {0} and {1} or {0} and {2}, the result is {0}".format(0,1,2))

Isn't this nice!!!这不是很好吗!!!

http://docs.python.org/release/3.1.3/library/stdtypes.html#str.format http://docs.python.org/release/3.1.3/library/stdtypes.html#str.format

Perform a string formatting operation.执行字符串格式化操作。 The format_string argument can contain literal text or replacement fields delimited by braces {}. format_string 参数可以包含由大括号 {} 分隔的文字文本或替换字段。 Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument.每个替换字段都包含位置参数的数字索引或关键字参数的名称。 Returns a copy of format_string where each replacement field is replaced with the string value of the corresponding argument.返回 format_string 的副本,其中每个替换字段都替换为相应参数的字符串值。

It's a placeholder which will be replaced with the first argument to format in the result.它是一个占位符,将替换为结果中format的第一个参数。 {1} would be the second argument and so on. {1}将是第二个参数,依此类推。

See Format String Syntax for details.有关详细信息,请参阅格式化字符串语法

That is the new python formatting style.那是新的 python 格式化样式。 Read up on ithere .这里阅读它。

year = int(input("Enter the year: "))
if year%4 == 0:
    if year%100 == 0:
        if year%400 == 0:
            print("{0} Year is Leap Year".format(year))
        else:
            print("{0} Year is Not Leap Year".format(year))
    else:
        print("{0} Year is Leap Year".format(year))
else:
    print("{0} Year is Not Leap Year".format(year))

here I can place the year argument in multiple lines using .format(year)在这里,我可以使用.format(year)year参数放在多行中

output: output:

> Enter the year: 1996
> 1996 Year is Leap Year

AND Another example: AND 另一个例子:

name = 'sagar'
place = 'hyd'
greet = 'Good'
print("my name is {0}. I am from {1}. Hope everyone doing {2}".format(name,place,greet))

output: output:

> my name is sagar.
> I am from hyd. Hope everyone doing Good

OR或者

print("my name is {0}. I am from {1}. Hope everyone doing {2}".format('Sagar','Hyd','Good'))

Output: Output:

> my name is sagar. I am from hyd. Hope everyone doing Good

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM