Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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))

Solution 2:

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!!!

Solution 3:

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 {}. 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.

Solution 4:

It's a placeholder which will be replaced with the first argument to format in the result. {1} would be the second argument and so on.

See Format String Syntax for details.

Solution 5:

That is the new python formatting style. Read up on it here.

Post a Comment for "What Does {0} Mean In This Python String?"