What does {0} mean in this Python string?












18














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!










share|improve this question





























    18














    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!










    share|improve this question



























      18












      18








      18


      8





      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!










      share|improve this question















      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!







      python python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 13 '11 at 18:53









      Mu Mind

      8,34112657




      8,34112657










      asked Jul 13 '11 at 17:09









      Elizabeth

      94113




      94113
























          6 Answers
          6






          active

          oldest

          votes


















          30














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






          share|improve this answer



















          • 1




            Great, thank you! That makes sense.
            – Elizabeth
            Jul 13 '11 at 17:17



















          12














          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.







          share|improve this answer























          • +1 for linking to the explanation that op couldn't seem to find.
            – SingleNegationElimination
            Jul 13 '11 at 17:41












          • +1 for linking to the docs that the answer on top (with three times the number of votes!) didn't bother pointing to.
            – Lauritz V. Thaulow
            Jul 13 '11 at 17:53










          • Thanks for the link, it makes the concept very clear.
            – Elizabeth
            Jul 13 '11 at 19:31



















          9














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






          share|improve this answer



















          • 1




            It's nice, except that when you add 0 to 1, the result is 1, not 0;)
            – towi_parallelism
            Aug 16 '16 at 14:58



















          6














          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.






          share|improve this answer





























            4














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






            share|improve this answer





























              0














              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 .foramt(year)



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



              AND Another ex:



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


              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: my name is sagar. I am from hyd. Hope everyone doing Good






              share|improve this answer





















                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6682806%2fwhat-does-0-mean-in-this-python-string%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                6 Answers
                6






                active

                oldest

                votes








                6 Answers
                6






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                30














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






                share|improve this answer



















                • 1




                  Great, thank you! That makes sense.
                  – Elizabeth
                  Jul 13 '11 at 17:17
















                30














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






                share|improve this answer



















                • 1




                  Great, thank you! That makes sense.
                  – Elizabeth
                  Jul 13 '11 at 17:17














                30












                30








                30






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






                share|improve this answer














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







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 19 '14 at 16:08









                rightfold

                23.5k66992




                23.5k66992










                answered Jul 13 '11 at 17:11









                0x5f3759df

                1,66911624




                1,66911624








                • 1




                  Great, thank you! That makes sense.
                  – Elizabeth
                  Jul 13 '11 at 17:17














                • 1




                  Great, thank you! That makes sense.
                  – Elizabeth
                  Jul 13 '11 at 17:17








                1




                1




                Great, thank you! That makes sense.
                – Elizabeth
                Jul 13 '11 at 17:17




                Great, thank you! That makes sense.
                – Elizabeth
                Jul 13 '11 at 17:17













                12














                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.







                share|improve this answer























                • +1 for linking to the explanation that op couldn't seem to find.
                  – SingleNegationElimination
                  Jul 13 '11 at 17:41












                • +1 for linking to the docs that the answer on top (with three times the number of votes!) didn't bother pointing to.
                  – Lauritz V. Thaulow
                  Jul 13 '11 at 17:53










                • Thanks for the link, it makes the concept very clear.
                  – Elizabeth
                  Jul 13 '11 at 19:31
















                12














                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.







                share|improve this answer























                • +1 for linking to the explanation that op couldn't seem to find.
                  – SingleNegationElimination
                  Jul 13 '11 at 17:41












                • +1 for linking to the docs that the answer on top (with three times the number of votes!) didn't bother pointing to.
                  – Lauritz V. Thaulow
                  Jul 13 '11 at 17:53










                • Thanks for the link, it makes the concept very clear.
                  – Elizabeth
                  Jul 13 '11 at 19:31














                12












                12








                12






                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.







                share|improve this answer














                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.








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jul 14 '14 at 9:18









                fedorqui

                165k51332377




                165k51332377










                answered Jul 13 '11 at 17:12









                tMC

                7,69094791




                7,69094791












                • +1 for linking to the explanation that op couldn't seem to find.
                  – SingleNegationElimination
                  Jul 13 '11 at 17:41












                • +1 for linking to the docs that the answer on top (with three times the number of votes!) didn't bother pointing to.
                  – Lauritz V. Thaulow
                  Jul 13 '11 at 17:53










                • Thanks for the link, it makes the concept very clear.
                  – Elizabeth
                  Jul 13 '11 at 19:31


















                • +1 for linking to the explanation that op couldn't seem to find.
                  – SingleNegationElimination
                  Jul 13 '11 at 17:41












                • +1 for linking to the docs that the answer on top (with three times the number of votes!) didn't bother pointing to.
                  – Lauritz V. Thaulow
                  Jul 13 '11 at 17:53










                • Thanks for the link, it makes the concept very clear.
                  – Elizabeth
                  Jul 13 '11 at 19:31
















                +1 for linking to the explanation that op couldn't seem to find.
                – SingleNegationElimination
                Jul 13 '11 at 17:41






                +1 for linking to the explanation that op couldn't seem to find.
                – SingleNegationElimination
                Jul 13 '11 at 17:41














                +1 for linking to the docs that the answer on top (with three times the number of votes!) didn't bother pointing to.
                – Lauritz V. Thaulow
                Jul 13 '11 at 17:53




                +1 for linking to the docs that the answer on top (with three times the number of votes!) didn't bother pointing to.
                – Lauritz V. Thaulow
                Jul 13 '11 at 17:53












                Thanks for the link, it makes the concept very clear.
                – Elizabeth
                Jul 13 '11 at 19:31




                Thanks for the link, it makes the concept very clear.
                – Elizabeth
                Jul 13 '11 at 19:31











                9














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






                share|improve this answer



















                • 1




                  It's nice, except that when you add 0 to 1, the result is 1, not 0;)
                  – towi_parallelism
                  Aug 16 '16 at 14:58
















                9














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






                share|improve this answer



















                • 1




                  It's nice, except that when you add 0 to 1, the result is 1, not 0;)
                  – towi_parallelism
                  Aug 16 '16 at 14:58














                9












                9








                9






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






                share|improve this answer














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







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 7 '16 at 17:42

























                answered Jul 20 '12 at 6:57









                Dave A

                1861312




                1861312








                • 1




                  It's nice, except that when you add 0 to 1, the result is 1, not 0;)
                  – towi_parallelism
                  Aug 16 '16 at 14:58














                • 1




                  It's nice, except that when you add 0 to 1, the result is 1, not 0;)
                  – towi_parallelism
                  Aug 16 '16 at 14:58








                1




                1




                It's nice, except that when you add 0 to 1, the result is 1, not 0;)
                – towi_parallelism
                Aug 16 '16 at 14:58




                It's nice, except that when you add 0 to 1, the result is 1, not 0;)
                – towi_parallelism
                Aug 16 '16 at 14:58











                6














                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.






                share|improve this answer


























                  6














                  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.






                  share|improve this answer
























                    6












                    6








                    6






                    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.






                    share|improve this answer












                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 13 '11 at 17:13









                    hammar

                    125k16262360




                    125k16262360























                        4














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






                        share|improve this answer


























                          4














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






                          share|improve this answer
























                            4












                            4








                            4






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






                            share|improve this answer












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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jul 13 '11 at 17:12









                            orlp

                            66.8k26159246




                            66.8k26159246























                                0














                                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 .foramt(year)



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



                                AND Another ex:



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


                                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: my name is sagar. I am from hyd. Hope everyone doing Good






                                share|improve this answer


























                                  0














                                  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 .foramt(year)



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



                                  AND Another ex:



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


                                  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: my name is sagar. I am from hyd. Hope everyone doing Good






                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    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 .foramt(year)



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



                                    AND Another ex:



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


                                    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: my name is sagar. I am from hyd. Hope everyone doing Good






                                    share|improve this answer












                                    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 .foramt(year)



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



                                    AND Another ex:



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


                                    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: my name is sagar. I am from hyd. Hope everyone doing Good







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 19 '18 at 12:39









                                    Dikshit Sagar

                                    31




                                    31






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6682806%2fwhat-does-0-mean-in-this-python-string%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                        SQL update select statement

                                        'app-layout' is not a known element: how to share Component with different Modules