Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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!

like image 360
Elizabeth Avatar asked Jul 13 '11 17:07

Elizabeth


People also ask

What does a 0 mean in Python?

It acts as an indicator in the format method that if you want it to be replaced by the first parameter(index zero) of format. Example : print(42+261={0}.format(303)) Here, {0} will be replaced by 303.

What is a [: 0 1 mean in Python?

It is a mathematical notation for an "open range" or "half closed interval". The notation has no use in common programming languages, including Python.

How do you add 0 to a string in Python?

Python String zfill() MethodThe zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length. If the value of the len parameter is less than the length of the string, no filling is done.

Is 0 treated as false in Python?

Numbers. In Python, the integer 0 is always False , while every other number, including negative numbers, are True .


1 Answers

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

like image 117
0x5f3759df Avatar answered Sep 19 '22 21:09

0x5f3759df