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!
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.
It is a mathematical notation for an "open range" or "half closed interval". The notation has no use in common programming languages, including 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.
Numbers. In Python, the integer 0 is always False , while every other number, including negative numbers, are True .
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)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With