convert number to string in python











up vote
-1
down vote

favorite












I meet a problem about converting number to string.



I want to get a string "0100" from str(0100), but what I got is "64". Does any way I can do to get a string "0100" from 0100.



UPDATE



Thanks for many people's pointing out that the leading "0" is a indicator for octal. I know it, what I want is to convert 0100 to "0100", any suggestion?



Best Regards,










share|improve this question
























  • Re your update - you could just use oct(0100) but that's probably not your intention. Where are you obtaining the integer 0100 from? Are you reading it from somewhere? Perhaps there's better ways to solve this, but you'll need to provide us with some context to your problem.
    – del
    Nov 3 '12 at 7:35

















up vote
-1
down vote

favorite












I meet a problem about converting number to string.



I want to get a string "0100" from str(0100), but what I got is "64". Does any way I can do to get a string "0100" from 0100.



UPDATE



Thanks for many people's pointing out that the leading "0" is a indicator for octal. I know it, what I want is to convert 0100 to "0100", any suggestion?



Best Regards,










share|improve this question
























  • Re your update - you could just use oct(0100) but that's probably not your intention. Where are you obtaining the integer 0100 from? Are you reading it from somewhere? Perhaps there's better ways to solve this, but you'll need to provide us with some context to your problem.
    – del
    Nov 3 '12 at 7:35















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I meet a problem about converting number to string.



I want to get a string "0100" from str(0100), but what I got is "64". Does any way I can do to get a string "0100" from 0100.



UPDATE



Thanks for many people's pointing out that the leading "0" is a indicator for octal. I know it, what I want is to convert 0100 to "0100", any suggestion?



Best Regards,










share|improve this question















I meet a problem about converting number to string.



I want to get a string "0100" from str(0100), but what I got is "64". Does any way I can do to get a string "0100" from 0100.



UPDATE



Thanks for many people's pointing out that the leading "0" is a indicator for octal. I know it, what I want is to convert 0100 to "0100", any suggestion?



Best Regards,







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 3 '12 at 8:23

























asked Nov 3 '12 at 6:36









Yongwei Xing

4,919195782




4,919195782












  • Re your update - you could just use oct(0100) but that's probably not your intention. Where are you obtaining the integer 0100 from? Are you reading it from somewhere? Perhaps there's better ways to solve this, but you'll need to provide us with some context to your problem.
    – del
    Nov 3 '12 at 7:35




















  • Re your update - you could just use oct(0100) but that's probably not your intention. Where are you obtaining the integer 0100 from? Are you reading it from somewhere? Perhaps there's better ways to solve this, but you'll need to provide us with some context to your problem.
    – del
    Nov 3 '12 at 7:35


















Re your update - you could just use oct(0100) but that's probably not your intention. Where are you obtaining the integer 0100 from? Are you reading it from somewhere? Perhaps there's better ways to solve this, but you'll need to provide us with some context to your problem.
– del
Nov 3 '12 at 7:35






Re your update - you could just use oct(0100) but that's probably not your intention. Where are you obtaining the integer 0100 from? Are you reading it from somewhere? Perhaps there's better ways to solve this, but you'll need to provide us with some context to your problem.
– del
Nov 3 '12 at 7:35














5 Answers
5






active

oldest

votes

















up vote
-1
down vote



accepted










Not sure if I understood the question... maybe you're looking for this:



'%04o' % 0100  # 0100





share|improve this answer

















  • 1




    This solution breaks for many other cases. e.g. '%04o' % 1100 returns "2114"
    – del
    Nov 4 '12 at 2:35










  • This is Python2 Syntax and won't work with CPython from 2020. Don't teach this to people! Python3 way is using f-strings (e.g. f"{100:04o}"
    – SV-97
    Nov 18 at 22:15










  • @SV-97: look at the post date, dude
    – georg
    Nov 18 at 23:48










  • @georg Well Shit. Why was this in my feed then?
    – SV-97
    Nov 19 at 5:53










  • @SV-97 Because someone added an answer 8 hours ago.
    – FrankerZ
    Nov 19 at 7:04




















up vote
1
down vote













Your first issue is that the literal "0100", because it begins with a digit 0, is interpreted in octal instead of decimal. By contrast, str(100) returns "100" as expected.



Secondly, it sounds like you want to zero-fill your numbers to a fixed width, which you can do with the zfill method on strings. For example, str(100).zfill(4) returns "0100".






share|improve this answer




























    up vote
    1
    down vote













    The integer should be written as 100, not 0100.



    You can format the string with leading zeros like this:




    >>> "%04d" % 100
    '0100'





    share|improve this answer




























      up vote
      0
      down vote













      In Python2, a leading 0 on a numeric literal signifies that it is an octal number. This is why 0100 == 64. This also means that 0800 and 0900 are syntax errors



      It's a funny thing that's caught out many people at one time or another.



      In Python3, 0100 is a syntax error, you must use the 0o prefix if you need to write a octal literal. eg. 0o100 == 64






      share|improve this answer




























        up vote
        0
        down vote













        You could just concatenate both strings so if you want 0100



        res = str(0) + str(100)


        that way res = '0100'






        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',
          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%2f13206698%2fconvert-number-to-string-in-python%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          -1
          down vote



          accepted










          Not sure if I understood the question... maybe you're looking for this:



          '%04o' % 0100  # 0100





          share|improve this answer

















          • 1




            This solution breaks for many other cases. e.g. '%04o' % 1100 returns "2114"
            – del
            Nov 4 '12 at 2:35










          • This is Python2 Syntax and won't work with CPython from 2020. Don't teach this to people! Python3 way is using f-strings (e.g. f"{100:04o}"
            – SV-97
            Nov 18 at 22:15










          • @SV-97: look at the post date, dude
            – georg
            Nov 18 at 23:48










          • @georg Well Shit. Why was this in my feed then?
            – SV-97
            Nov 19 at 5:53










          • @SV-97 Because someone added an answer 8 hours ago.
            – FrankerZ
            Nov 19 at 7:04

















          up vote
          -1
          down vote



          accepted










          Not sure if I understood the question... maybe you're looking for this:



          '%04o' % 0100  # 0100





          share|improve this answer

















          • 1




            This solution breaks for many other cases. e.g. '%04o' % 1100 returns "2114"
            – del
            Nov 4 '12 at 2:35










          • This is Python2 Syntax and won't work with CPython from 2020. Don't teach this to people! Python3 way is using f-strings (e.g. f"{100:04o}"
            – SV-97
            Nov 18 at 22:15










          • @SV-97: look at the post date, dude
            – georg
            Nov 18 at 23:48










          • @georg Well Shit. Why was this in my feed then?
            – SV-97
            Nov 19 at 5:53










          • @SV-97 Because someone added an answer 8 hours ago.
            – FrankerZ
            Nov 19 at 7:04















          up vote
          -1
          down vote



          accepted







          up vote
          -1
          down vote



          accepted






          Not sure if I understood the question... maybe you're looking for this:



          '%04o' % 0100  # 0100





          share|improve this answer












          Not sure if I understood the question... maybe you're looking for this:



          '%04o' % 0100  # 0100






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 3 '12 at 10:32









          georg

          143k33193290




          143k33193290








          • 1




            This solution breaks for many other cases. e.g. '%04o' % 1100 returns "2114"
            – del
            Nov 4 '12 at 2:35










          • This is Python2 Syntax and won't work with CPython from 2020. Don't teach this to people! Python3 way is using f-strings (e.g. f"{100:04o}"
            – SV-97
            Nov 18 at 22:15










          • @SV-97: look at the post date, dude
            – georg
            Nov 18 at 23:48










          • @georg Well Shit. Why was this in my feed then?
            – SV-97
            Nov 19 at 5:53










          • @SV-97 Because someone added an answer 8 hours ago.
            – FrankerZ
            Nov 19 at 7:04
















          • 1




            This solution breaks for many other cases. e.g. '%04o' % 1100 returns "2114"
            – del
            Nov 4 '12 at 2:35










          • This is Python2 Syntax and won't work with CPython from 2020. Don't teach this to people! Python3 way is using f-strings (e.g. f"{100:04o}"
            – SV-97
            Nov 18 at 22:15










          • @SV-97: look at the post date, dude
            – georg
            Nov 18 at 23:48










          • @georg Well Shit. Why was this in my feed then?
            – SV-97
            Nov 19 at 5:53










          • @SV-97 Because someone added an answer 8 hours ago.
            – FrankerZ
            Nov 19 at 7:04










          1




          1




          This solution breaks for many other cases. e.g. '%04o' % 1100 returns "2114"
          – del
          Nov 4 '12 at 2:35




          This solution breaks for many other cases. e.g. '%04o' % 1100 returns "2114"
          – del
          Nov 4 '12 at 2:35












          This is Python2 Syntax and won't work with CPython from 2020. Don't teach this to people! Python3 way is using f-strings (e.g. f"{100:04o}"
          – SV-97
          Nov 18 at 22:15




          This is Python2 Syntax and won't work with CPython from 2020. Don't teach this to people! Python3 way is using f-strings (e.g. f"{100:04o}"
          – SV-97
          Nov 18 at 22:15












          @SV-97: look at the post date, dude
          – georg
          Nov 18 at 23:48




          @SV-97: look at the post date, dude
          – georg
          Nov 18 at 23:48












          @georg Well Shit. Why was this in my feed then?
          – SV-97
          Nov 19 at 5:53




          @georg Well Shit. Why was this in my feed then?
          – SV-97
          Nov 19 at 5:53












          @SV-97 Because someone added an answer 8 hours ago.
          – FrankerZ
          Nov 19 at 7:04






          @SV-97 Because someone added an answer 8 hours ago.
          – FrankerZ
          Nov 19 at 7:04














          up vote
          1
          down vote













          Your first issue is that the literal "0100", because it begins with a digit 0, is interpreted in octal instead of decimal. By contrast, str(100) returns "100" as expected.



          Secondly, it sounds like you want to zero-fill your numbers to a fixed width, which you can do with the zfill method on strings. For example, str(100).zfill(4) returns "0100".






          share|improve this answer

























            up vote
            1
            down vote













            Your first issue is that the literal "0100", because it begins with a digit 0, is interpreted in octal instead of decimal. By contrast, str(100) returns "100" as expected.



            Secondly, it sounds like you want to zero-fill your numbers to a fixed width, which you can do with the zfill method on strings. For example, str(100).zfill(4) returns "0100".






            share|improve this answer























              up vote
              1
              down vote










              up vote
              1
              down vote









              Your first issue is that the literal "0100", because it begins with a digit 0, is interpreted in octal instead of decimal. By contrast, str(100) returns "100" as expected.



              Secondly, it sounds like you want to zero-fill your numbers to a fixed width, which you can do with the zfill method on strings. For example, str(100).zfill(4) returns "0100".






              share|improve this answer












              Your first issue is that the literal "0100", because it begins with a digit 0, is interpreted in octal instead of decimal. By contrast, str(100) returns "100" as expected.



              Secondly, it sounds like you want to zero-fill your numbers to a fixed width, which you can do with the zfill method on strings. For example, str(100).zfill(4) returns "0100".







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 3 '12 at 6:41









              Jamey Sharp

              6,70221940




              6,70221940






















                  up vote
                  1
                  down vote













                  The integer should be written as 100, not 0100.



                  You can format the string with leading zeros like this:




                  >>> "%04d" % 100
                  '0100'





                  share|improve this answer

























                    up vote
                    1
                    down vote













                    The integer should be written as 100, not 0100.



                    You can format the string with leading zeros like this:




                    >>> "%04d" % 100
                    '0100'





                    share|improve this answer























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      The integer should be written as 100, not 0100.



                      You can format the string with leading zeros like this:




                      >>> "%04d" % 100
                      '0100'





                      share|improve this answer












                      The integer should be written as 100, not 0100.



                      You can format the string with leading zeros like this:




                      >>> "%04d" % 100
                      '0100'






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 3 '12 at 6:41









                      del

                      2,98873342




                      2,98873342






















                          up vote
                          0
                          down vote













                          In Python2, a leading 0 on a numeric literal signifies that it is an octal number. This is why 0100 == 64. This also means that 0800 and 0900 are syntax errors



                          It's a funny thing that's caught out many people at one time or another.



                          In Python3, 0100 is a syntax error, you must use the 0o prefix if you need to write a octal literal. eg. 0o100 == 64






                          share|improve this answer

























                            up vote
                            0
                            down vote













                            In Python2, a leading 0 on a numeric literal signifies that it is an octal number. This is why 0100 == 64. This also means that 0800 and 0900 are syntax errors



                            It's a funny thing that's caught out many people at one time or another.



                            In Python3, 0100 is a syntax error, you must use the 0o prefix if you need to write a octal literal. eg. 0o100 == 64






                            share|improve this answer























                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              In Python2, a leading 0 on a numeric literal signifies that it is an octal number. This is why 0100 == 64. This also means that 0800 and 0900 are syntax errors



                              It's a funny thing that's caught out many people at one time or another.



                              In Python3, 0100 is a syntax error, you must use the 0o prefix if you need to write a octal literal. eg. 0o100 == 64






                              share|improve this answer












                              In Python2, a leading 0 on a numeric literal signifies that it is an octal number. This is why 0100 == 64. This also means that 0800 and 0900 are syntax errors



                              It's a funny thing that's caught out many people at one time or another.



                              In Python3, 0100 is a syntax error, you must use the 0o prefix if you need to write a octal literal. eg. 0o100 == 64







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 3 '12 at 6:48









                              John La Rooy

                              206k37270425




                              206k37270425






















                                  up vote
                                  0
                                  down vote













                                  You could just concatenate both strings so if you want 0100



                                  res = str(0) + str(100)


                                  that way res = '0100'






                                  share|improve this answer



























                                    up vote
                                    0
                                    down vote













                                    You could just concatenate both strings so if you want 0100



                                    res = str(0) + str(100)


                                    that way res = '0100'






                                    share|improve this answer

























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      You could just concatenate both strings so if you want 0100



                                      res = str(0) + str(100)


                                      that way res = '0100'






                                      share|improve this answer














                                      You could just concatenate both strings so if you want 0100



                                      res = str(0) + str(100)


                                      that way res = '0100'







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 19 at 7:03









                                      FrankerZ

                                      15.5k72859




                                      15.5k72859










                                      answered Nov 18 at 22:03









                                      MedoAlmasry

                                      498




                                      498






























                                           

                                          draft saved


                                          draft discarded



















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13206698%2fconvert-number-to-string-in-python%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

                                          Costa Masnaga

                                          Fotorealismo

                                          Sidney Franklin