Assigning a variable to part of a value





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















So I am writing a program in python3 and Im stuck on one part. My problem is I have a variable. example:



    variable = 12345


so I want to assign a specific variable to one part of the value of the other variable.



Ive tried example:



    variable[2] = 55 


(this is an example but hopefully im coming off clear enough)
so I want to take the 3rd digit in "12345" 12[3]45
so the "3" I want to assign a variable basically from a place inside another variable.
What sucks is it has to be inside that variable. Ive thought about passing it as a string instead but I think that will change the whole rest of the script being that now its a string.
so I thought also about each time I need to call that variable I can pass it as a string to another variable but im trying not to get swamped with a million str and int swaps. I hope Ive made this understandable...I get that its not very clear lol but Ive confused my self with this script any help will be much appreciated :)










share|improve this question































    2















    So I am writing a program in python3 and Im stuck on one part. My problem is I have a variable. example:



        variable = 12345


    so I want to assign a specific variable to one part of the value of the other variable.



    Ive tried example:



        variable[2] = 55 


    (this is an example but hopefully im coming off clear enough)
    so I want to take the 3rd digit in "12345" 12[3]45
    so the "3" I want to assign a variable basically from a place inside another variable.
    What sucks is it has to be inside that variable. Ive thought about passing it as a string instead but I think that will change the whole rest of the script being that now its a string.
    so I thought also about each time I need to call that variable I can pass it as a string to another variable but im trying not to get swamped with a million str and int swaps. I hope Ive made this understandable...I get that its not very clear lol but Ive confused my self with this script any help will be much appreciated :)










    share|improve this question



























      2












      2








      2








      So I am writing a program in python3 and Im stuck on one part. My problem is I have a variable. example:



          variable = 12345


      so I want to assign a specific variable to one part of the value of the other variable.



      Ive tried example:



          variable[2] = 55 


      (this is an example but hopefully im coming off clear enough)
      so I want to take the 3rd digit in "12345" 12[3]45
      so the "3" I want to assign a variable basically from a place inside another variable.
      What sucks is it has to be inside that variable. Ive thought about passing it as a string instead but I think that will change the whole rest of the script being that now its a string.
      so I thought also about each time I need to call that variable I can pass it as a string to another variable but im trying not to get swamped with a million str and int swaps. I hope Ive made this understandable...I get that its not very clear lol but Ive confused my self with this script any help will be much appreciated :)










      share|improve this question
















      So I am writing a program in python3 and Im stuck on one part. My problem is I have a variable. example:



          variable = 12345


      so I want to assign a specific variable to one part of the value of the other variable.



      Ive tried example:



          variable[2] = 55 


      (this is an example but hopefully im coming off clear enough)
      so I want to take the 3rd digit in "12345" 12[3]45
      so the "3" I want to assign a variable basically from a place inside another variable.
      What sucks is it has to be inside that variable. Ive thought about passing it as a string instead but I think that will change the whole rest of the script being that now its a string.
      so I thought also about each time I need to call that variable I can pass it as a string to another variable but im trying not to get swamped with a million str and int swaps. I hope Ive made this understandable...I get that its not very clear lol but Ive confused my self with this script any help will be much appreciated :)







      python python-3.x list






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 29 '18 at 9:42









      Ivan Kolesnikov

      1,27111133




      1,27111133










      asked Nov 29 '18 at 2:32









      stefan Valentinostefan Valentino

      111




      111
























          2 Answers
          2






          active

          oldest

          votes


















          0














          In any case we have to perform conversions to strings and lists.
          We can achieve this in compatible way with python2.x and python3.x as follow:



          # input variable
          variable = 12345
          # convert that variable to list
          variable_lst = [int(i) for i in str(variable)]
          # change necessary digit by list index
          variable_lst[2] = 55
          # reverse conversion then assign updated data to variable
          variable = int(''.join(str(j) for j in variable_lst)) # 125545





          share|improve this answer































            0














            you should convert your variable to list type, so you could change any index of it:



            var = 12345
            var_list= list(str(var))
            var_list[2]=55
            var_list= int(''.join(str(i) for i in var_list))





            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%2f53530990%2fassigning-a-variable-to-part-of-a-value%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              In any case we have to perform conversions to strings and lists.
              We can achieve this in compatible way with python2.x and python3.x as follow:



              # input variable
              variable = 12345
              # convert that variable to list
              variable_lst = [int(i) for i in str(variable)]
              # change necessary digit by list index
              variable_lst[2] = 55
              # reverse conversion then assign updated data to variable
              variable = int(''.join(str(j) for j in variable_lst)) # 125545





              share|improve this answer




























                0














                In any case we have to perform conversions to strings and lists.
                We can achieve this in compatible way with python2.x and python3.x as follow:



                # input variable
                variable = 12345
                # convert that variable to list
                variable_lst = [int(i) for i in str(variable)]
                # change necessary digit by list index
                variable_lst[2] = 55
                # reverse conversion then assign updated data to variable
                variable = int(''.join(str(j) for j in variable_lst)) # 125545





                share|improve this answer


























                  0












                  0








                  0







                  In any case we have to perform conversions to strings and lists.
                  We can achieve this in compatible way with python2.x and python3.x as follow:



                  # input variable
                  variable = 12345
                  # convert that variable to list
                  variable_lst = [int(i) for i in str(variable)]
                  # change necessary digit by list index
                  variable_lst[2] = 55
                  # reverse conversion then assign updated data to variable
                  variable = int(''.join(str(j) for j in variable_lst)) # 125545





                  share|improve this answer













                  In any case we have to perform conversions to strings and lists.
                  We can achieve this in compatible way with python2.x and python3.x as follow:



                  # input variable
                  variable = 12345
                  # convert that variable to list
                  variable_lst = [int(i) for i in str(variable)]
                  # change necessary digit by list index
                  variable_lst[2] = 55
                  # reverse conversion then assign updated data to variable
                  variable = int(''.join(str(j) for j in variable_lst)) # 125545






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 29 '18 at 9:20









                  Ivan KolesnikovIvan Kolesnikov

                  1,27111133




                  1,27111133

























                      0














                      you should convert your variable to list type, so you could change any index of it:



                      var = 12345
                      var_list= list(str(var))
                      var_list[2]=55
                      var_list= int(''.join(str(i) for i in var_list))





                      share|improve this answer




























                        0














                        you should convert your variable to list type, so you could change any index of it:



                        var = 12345
                        var_list= list(str(var))
                        var_list[2]=55
                        var_list= int(''.join(str(i) for i in var_list))





                        share|improve this answer


























                          0












                          0








                          0







                          you should convert your variable to list type, so you could change any index of it:



                          var = 12345
                          var_list= list(str(var))
                          var_list[2]=55
                          var_list= int(''.join(str(i) for i in var_list))





                          share|improve this answer













                          you should convert your variable to list type, so you could change any index of it:



                          var = 12345
                          var_list= list(str(var))
                          var_list[2]=55
                          var_list= int(''.join(str(i) for i in var_list))






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 25 at 20:45









                          danoosh.nozaridanoosh.nozari

                          11




                          11






























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53530990%2fassigning-a-variable-to-part-of-a-value%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

                              Lallio

                              Futebolista

                              Jornalista