String concatenate TypeError: can only concatenate str (not “int”) to str"





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







1















I am following a tutorial and I am getting an error.



My code should be this:



salaries = {'John':'20','Sally':'30','Sammy':'15'}
print(salaries['John'])

salaries['John'] = salaries['John'] + 30
print(salaries['John'])


I am getting back an error like this




Traceback (most recent call last): File "print.py", line 9, in

salaries['John'] = salaries['John'] + 30 TypeError: can only concatenate str (not "int") to str




Can you help me with this?










share|improve this question

























  • Sorry for not knowing how to post a code . It's my first time here.

    – Alex
    Nov 25 '18 at 8:47











  • salaries is a dictionary and you try to only add 1 value, try adding a key-value pair.

    – Tobias Wilfert
    Nov 25 '18 at 8:48











  • you cannot multiply or add anything with/to print function output

    – Khalil Al Hooti
    Nov 25 '18 at 8:48











  • Could you maybe add your desired outcome because right now it is not really clear what you want to do?

    – Tobias Wilfert
    Nov 25 '18 at 8:49











  • Thank you , it's a python 3.7 thing cause the guy in the tutorial is making this exactly like I wrote.

    – Alex
    Nov 25 '18 at 8:49


















1















I am following a tutorial and I am getting an error.



My code should be this:



salaries = {'John':'20','Sally':'30','Sammy':'15'}
print(salaries['John'])

salaries['John'] = salaries['John'] + 30
print(salaries['John'])


I am getting back an error like this




Traceback (most recent call last): File "print.py", line 9, in

salaries['John'] = salaries['John'] + 30 TypeError: can only concatenate str (not "int") to str




Can you help me with this?










share|improve this question

























  • Sorry for not knowing how to post a code . It's my first time here.

    – Alex
    Nov 25 '18 at 8:47











  • salaries is a dictionary and you try to only add 1 value, try adding a key-value pair.

    – Tobias Wilfert
    Nov 25 '18 at 8:48











  • you cannot multiply or add anything with/to print function output

    – Khalil Al Hooti
    Nov 25 '18 at 8:48











  • Could you maybe add your desired outcome because right now it is not really clear what you want to do?

    – Tobias Wilfert
    Nov 25 '18 at 8:49











  • Thank you , it's a python 3.7 thing cause the guy in the tutorial is making this exactly like I wrote.

    – Alex
    Nov 25 '18 at 8:49














1












1








1








I am following a tutorial and I am getting an error.



My code should be this:



salaries = {'John':'20','Sally':'30','Sammy':'15'}
print(salaries['John'])

salaries['John'] = salaries['John'] + 30
print(salaries['John'])


I am getting back an error like this




Traceback (most recent call last): File "print.py", line 9, in

salaries['John'] = salaries['John'] + 30 TypeError: can only concatenate str (not "int") to str




Can you help me with this?










share|improve this question
















I am following a tutorial and I am getting an error.



My code should be this:



salaries = {'John':'20','Sally':'30','Sammy':'15'}
print(salaries['John'])

salaries['John'] = salaries['John'] + 30
print(salaries['John'])


I am getting back an error like this




Traceback (most recent call last): File "print.py", line 9, in

salaries['John'] = salaries['John'] + 30 TypeError: can only concatenate str (not "int") to str




Can you help me with this?







python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 10:09









usr2564301

18k73374




18k73374










asked Nov 25 '18 at 8:44









AlexAlex

132




132













  • Sorry for not knowing how to post a code . It's my first time here.

    – Alex
    Nov 25 '18 at 8:47











  • salaries is a dictionary and you try to only add 1 value, try adding a key-value pair.

    – Tobias Wilfert
    Nov 25 '18 at 8:48











  • you cannot multiply or add anything with/to print function output

    – Khalil Al Hooti
    Nov 25 '18 at 8:48











  • Could you maybe add your desired outcome because right now it is not really clear what you want to do?

    – Tobias Wilfert
    Nov 25 '18 at 8:49











  • Thank you , it's a python 3.7 thing cause the guy in the tutorial is making this exactly like I wrote.

    – Alex
    Nov 25 '18 at 8:49



















  • Sorry for not knowing how to post a code . It's my first time here.

    – Alex
    Nov 25 '18 at 8:47











  • salaries is a dictionary and you try to only add 1 value, try adding a key-value pair.

    – Tobias Wilfert
    Nov 25 '18 at 8:48











  • you cannot multiply or add anything with/to print function output

    – Khalil Al Hooti
    Nov 25 '18 at 8:48











  • Could you maybe add your desired outcome because right now it is not really clear what you want to do?

    – Tobias Wilfert
    Nov 25 '18 at 8:49











  • Thank you , it's a python 3.7 thing cause the guy in the tutorial is making this exactly like I wrote.

    – Alex
    Nov 25 '18 at 8:49

















Sorry for not knowing how to post a code . It's my first time here.

– Alex
Nov 25 '18 at 8:47





Sorry for not knowing how to post a code . It's my first time here.

– Alex
Nov 25 '18 at 8:47













salaries is a dictionary and you try to only add 1 value, try adding a key-value pair.

– Tobias Wilfert
Nov 25 '18 at 8:48





salaries is a dictionary and you try to only add 1 value, try adding a key-value pair.

– Tobias Wilfert
Nov 25 '18 at 8:48













you cannot multiply or add anything with/to print function output

– Khalil Al Hooti
Nov 25 '18 at 8:48





you cannot multiply or add anything with/to print function output

– Khalil Al Hooti
Nov 25 '18 at 8:48













Could you maybe add your desired outcome because right now it is not really clear what you want to do?

– Tobias Wilfert
Nov 25 '18 at 8:49





Could you maybe add your desired outcome because right now it is not really clear what you want to do?

– Tobias Wilfert
Nov 25 '18 at 8:49













Thank you , it's a python 3.7 thing cause the guy in the tutorial is making this exactly like I wrote.

– Alex
Nov 25 '18 at 8:49





Thank you , it's a python 3.7 thing cause the guy in the tutorial is making this exactly like I wrote.

– Alex
Nov 25 '18 at 8:49












3 Answers
3






active

oldest

votes


















1














This should fix it:



salaries['John'] = str(int(salaries['John']) + 30)


You need to convert the salaries of John to an int add 30 and then convert it back to a string.



This will change salaries['John'] from 20 to 50






share|improve this answer































    1














    If you wanted to include the 30 you'd have to put something like str(30). That's why it's giving you that error cause 30 is an int and the rest are strings you can't combine strings and ints. Hope this helps






    share|improve this answer
























    • Thank you Alex , seems we're from the same country :) I tried that but at the end I get the first salary of John and then when changing 30 to str(30) I am getting the previous salaty put together like 2030.

      – Alex
      Nov 25 '18 at 8:53













    • Oh. I misunderstood. If you want to add them then leave the 30 as an int and then try this int(salaries['John']) to print that you would str() the whole thing. Where are you from?

      – Alex Tănăsescu
      Nov 25 '18 at 8:57













    • Thank you Alex, Tobias made a little bit more clear for noobs like me ;))) it's my first time among this python and I feel like an idiot .

      – Alex
      Nov 25 '18 at 8:59











    • No worries. We all have to start somewhere. Just be sure to accept the answer you like as it can help other beginners learn too.

      – Alex Tănăsescu
      Nov 25 '18 at 9:00





















    0














    The "+" operator is using for concatenate strings, adding numbers, etc.
    in your case you trying to add two integers but in your dictionary "salaries" the values are strings.
    you can convert the value to int, adding the numbers and then convert to string to store the value.



    Try this:



    salaries['John'] = str(int(salaries['John']) + 30)
    print(salaries['John'])





    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%2f53465931%2fstring-concatenate-typeerror-can-only-concatenate-str-not-int-to-str%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      This should fix it:



      salaries['John'] = str(int(salaries['John']) + 30)


      You need to convert the salaries of John to an int add 30 and then convert it back to a string.



      This will change salaries['John'] from 20 to 50






      share|improve this answer




























        1














        This should fix it:



        salaries['John'] = str(int(salaries['John']) + 30)


        You need to convert the salaries of John to an int add 30 and then convert it back to a string.



        This will change salaries['John'] from 20 to 50






        share|improve this answer


























          1












          1








          1







          This should fix it:



          salaries['John'] = str(int(salaries['John']) + 30)


          You need to convert the salaries of John to an int add 30 and then convert it back to a string.



          This will change salaries['John'] from 20 to 50






          share|improve this answer













          This should fix it:



          salaries['John'] = str(int(salaries['John']) + 30)


          You need to convert the salaries of John to an int add 30 and then convert it back to a string.



          This will change salaries['John'] from 20 to 50







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 8:52









          Tobias WilfertTobias Wilfert

          84931022




          84931022

























              1














              If you wanted to include the 30 you'd have to put something like str(30). That's why it's giving you that error cause 30 is an int and the rest are strings you can't combine strings and ints. Hope this helps






              share|improve this answer
























              • Thank you Alex , seems we're from the same country :) I tried that but at the end I get the first salary of John and then when changing 30 to str(30) I am getting the previous salaty put together like 2030.

                – Alex
                Nov 25 '18 at 8:53













              • Oh. I misunderstood. If you want to add them then leave the 30 as an int and then try this int(salaries['John']) to print that you would str() the whole thing. Where are you from?

                – Alex Tănăsescu
                Nov 25 '18 at 8:57













              • Thank you Alex, Tobias made a little bit more clear for noobs like me ;))) it's my first time among this python and I feel like an idiot .

                – Alex
                Nov 25 '18 at 8:59











              • No worries. We all have to start somewhere. Just be sure to accept the answer you like as it can help other beginners learn too.

                – Alex Tănăsescu
                Nov 25 '18 at 9:00


















              1














              If you wanted to include the 30 you'd have to put something like str(30). That's why it's giving you that error cause 30 is an int and the rest are strings you can't combine strings and ints. Hope this helps






              share|improve this answer
























              • Thank you Alex , seems we're from the same country :) I tried that but at the end I get the first salary of John and then when changing 30 to str(30) I am getting the previous salaty put together like 2030.

                – Alex
                Nov 25 '18 at 8:53













              • Oh. I misunderstood. If you want to add them then leave the 30 as an int and then try this int(salaries['John']) to print that you would str() the whole thing. Where are you from?

                – Alex Tănăsescu
                Nov 25 '18 at 8:57













              • Thank you Alex, Tobias made a little bit more clear for noobs like me ;))) it's my first time among this python and I feel like an idiot .

                – Alex
                Nov 25 '18 at 8:59











              • No worries. We all have to start somewhere. Just be sure to accept the answer you like as it can help other beginners learn too.

                – Alex Tănăsescu
                Nov 25 '18 at 9:00
















              1












              1








              1







              If you wanted to include the 30 you'd have to put something like str(30). That's why it's giving you that error cause 30 is an int and the rest are strings you can't combine strings and ints. Hope this helps






              share|improve this answer













              If you wanted to include the 30 you'd have to put something like str(30). That's why it's giving you that error cause 30 is an int and the rest are strings you can't combine strings and ints. Hope this helps







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 25 '18 at 8:51









              Alex TănăsescuAlex Tănăsescu

              6810




              6810













              • Thank you Alex , seems we're from the same country :) I tried that but at the end I get the first salary of John and then when changing 30 to str(30) I am getting the previous salaty put together like 2030.

                – Alex
                Nov 25 '18 at 8:53













              • Oh. I misunderstood. If you want to add them then leave the 30 as an int and then try this int(salaries['John']) to print that you would str() the whole thing. Where are you from?

                – Alex Tănăsescu
                Nov 25 '18 at 8:57













              • Thank you Alex, Tobias made a little bit more clear for noobs like me ;))) it's my first time among this python and I feel like an idiot .

                – Alex
                Nov 25 '18 at 8:59











              • No worries. We all have to start somewhere. Just be sure to accept the answer you like as it can help other beginners learn too.

                – Alex Tănăsescu
                Nov 25 '18 at 9:00





















              • Thank you Alex , seems we're from the same country :) I tried that but at the end I get the first salary of John and then when changing 30 to str(30) I am getting the previous salaty put together like 2030.

                – Alex
                Nov 25 '18 at 8:53













              • Oh. I misunderstood. If you want to add them then leave the 30 as an int and then try this int(salaries['John']) to print that you would str() the whole thing. Where are you from?

                – Alex Tănăsescu
                Nov 25 '18 at 8:57













              • Thank you Alex, Tobias made a little bit more clear for noobs like me ;))) it's my first time among this python and I feel like an idiot .

                – Alex
                Nov 25 '18 at 8:59











              • No worries. We all have to start somewhere. Just be sure to accept the answer you like as it can help other beginners learn too.

                – Alex Tănăsescu
                Nov 25 '18 at 9:00



















              Thank you Alex , seems we're from the same country :) I tried that but at the end I get the first salary of John and then when changing 30 to str(30) I am getting the previous salaty put together like 2030.

              – Alex
              Nov 25 '18 at 8:53







              Thank you Alex , seems we're from the same country :) I tried that but at the end I get the first salary of John and then when changing 30 to str(30) I am getting the previous salaty put together like 2030.

              – Alex
              Nov 25 '18 at 8:53















              Oh. I misunderstood. If you want to add them then leave the 30 as an int and then try this int(salaries['John']) to print that you would str() the whole thing. Where are you from?

              – Alex Tănăsescu
              Nov 25 '18 at 8:57







              Oh. I misunderstood. If you want to add them then leave the 30 as an int and then try this int(salaries['John']) to print that you would str() the whole thing. Where are you from?

              – Alex Tănăsescu
              Nov 25 '18 at 8:57















              Thank you Alex, Tobias made a little bit more clear for noobs like me ;))) it's my first time among this python and I feel like an idiot .

              – Alex
              Nov 25 '18 at 8:59





              Thank you Alex, Tobias made a little bit more clear for noobs like me ;))) it's my first time among this python and I feel like an idiot .

              – Alex
              Nov 25 '18 at 8:59













              No worries. We all have to start somewhere. Just be sure to accept the answer you like as it can help other beginners learn too.

              – Alex Tănăsescu
              Nov 25 '18 at 9:00







              No worries. We all have to start somewhere. Just be sure to accept the answer you like as it can help other beginners learn too.

              – Alex Tănăsescu
              Nov 25 '18 at 9:00













              0














              The "+" operator is using for concatenate strings, adding numbers, etc.
              in your case you trying to add two integers but in your dictionary "salaries" the values are strings.
              you can convert the value to int, adding the numbers and then convert to string to store the value.



              Try this:



              salaries['John'] = str(int(salaries['John']) + 30)
              print(salaries['John'])





              share|improve this answer






























                0














                The "+" operator is using for concatenate strings, adding numbers, etc.
                in your case you trying to add two integers but in your dictionary "salaries" the values are strings.
                you can convert the value to int, adding the numbers and then convert to string to store the value.



                Try this:



                salaries['John'] = str(int(salaries['John']) + 30)
                print(salaries['John'])





                share|improve this answer




























                  0












                  0








                  0







                  The "+" operator is using for concatenate strings, adding numbers, etc.
                  in your case you trying to add two integers but in your dictionary "salaries" the values are strings.
                  you can convert the value to int, adding the numbers and then convert to string to store the value.



                  Try this:



                  salaries['John'] = str(int(salaries['John']) + 30)
                  print(salaries['John'])





                  share|improve this answer















                  The "+" operator is using for concatenate strings, adding numbers, etc.
                  in your case you trying to add two integers but in your dictionary "salaries" the values are strings.
                  you can convert the value to int, adding the numbers and then convert to string to store the value.



                  Try this:



                  salaries['John'] = str(int(salaries['John']) + 30)
                  print(salaries['John'])






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 '18 at 9:06

























                  answered Nov 25 '18 at 8:58









                  asdfasdf

                  18110




                  18110






























                      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%2f53465931%2fstring-concatenate-typeerror-can-only-concatenate-str-not-int-to-str%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







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()