Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

In the following code, {0} is been used in a string and I'm not sure about it and how it works. I can't able to find it anywhere to 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))

1 Answer

0 votes
by (26.4k points)
edited by

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.  

If you want to learn more about Python? Come & Join: python course 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 7, 2021 in Python by laddulakshana (16.4k points)
0 votes
5 answers
0 votes
1 answer

Browse Categories

...