How can I delete comma at the end of the output in Python?





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







-2















I am trying to order a word's letters by alphabetically in Python. But there is a comma at the end of the output.(I tried ''.sort() command, it worked well but there is square brackets at the beginning and at the end of the output). The input and the output must be like this:



word
'd','o','r','w'


This is my code:



alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
word=str(input())
for i in alphabet:
for j in word:
if i==j:
print("'{}',".format(i),end='')


And this is my output:



word
'd','o','r','w',









share|improve this question


















  • 2





    Btw it’s easier to just output sorted(word), and then just print ‘,’.join(sorted(word))

    – janscas
    Nov 16 '18 at 17:44




















-2















I am trying to order a word's letters by alphabetically in Python. But there is a comma at the end of the output.(I tried ''.sort() command, it worked well but there is square brackets at the beginning and at the end of the output). The input and the output must be like this:



word
'd','o','r','w'


This is my code:



alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
word=str(input())
for i in alphabet:
for j in word:
if i==j:
print("'{}',".format(i),end='')


And this is my output:



word
'd','o','r','w',









share|improve this question


















  • 2





    Btw it’s easier to just output sorted(word), and then just print ‘,’.join(sorted(word))

    – janscas
    Nov 16 '18 at 17:44
















-2












-2








-2








I am trying to order a word's letters by alphabetically in Python. But there is a comma at the end of the output.(I tried ''.sort() command, it worked well but there is square brackets at the beginning and at the end of the output). The input and the output must be like this:



word
'd','o','r','w'


This is my code:



alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
word=str(input())
for i in alphabet:
for j in word:
if i==j:
print("'{}',".format(i),end='')


And this is my output:



word
'd','o','r','w',









share|improve this question














I am trying to order a word's letters by alphabetically in Python. But there is a comma at the end of the output.(I tried ''.sort() command, it worked well but there is square brackets at the beginning and at the end of the output). The input and the output must be like this:



word
'd','o','r','w'


This is my code:



alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
word=str(input())
for i in alphabet:
for j in word:
if i==j:
print("'{}',".format(i),end='')


And this is my output:



word
'd','o','r','w',






python python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 17:39









PhillyPhilly

765




765








  • 2





    Btw it’s easier to just output sorted(word), and then just print ‘,’.join(sorted(word))

    – janscas
    Nov 16 '18 at 17:44
















  • 2





    Btw it’s easier to just output sorted(word), and then just print ‘,’.join(sorted(word))

    – janscas
    Nov 16 '18 at 17:44










2




2





Btw it’s easier to just output sorted(word), and then just print ‘,’.join(sorted(word))

– janscas
Nov 16 '18 at 17:44







Btw it’s easier to just output sorted(word), and then just print ‘,’.join(sorted(word))

– janscas
Nov 16 '18 at 17:44














4 Answers
4






active

oldest

votes


















2














Python strings have a join() function:



ls = ['a','b','c']
print(",".join(ls)) # prints "a,b,c"


Python also has what is called a 'list comprehension', that you can use like so:



alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
word=str(input())
matches = [l for l in word if l in alphabet]
print(",".join(sorted(matches)))


All the list comprehension does is put l in the list if it is in alphabet. All the candidate ls are taken from the word variable.



sorted is a function that will do a simple sort (though more complex sorts are possible).



Finally; here are a few other fun options that all result in "a,b,c,d":



"a,b,c,d,"[:-1] . # list-slice
"a,b,c,d,".strip(",") . # String strip





share|improve this answer

































    2














    you store it in an array and then print it at the end



    alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
    word=str(input())
    matches =
    for i in alphabet:
    for j in word:
    if i==j:
    matches.append("'{i}',".format(i=i))
    #now that matches has all our matches
    print(",".join(arrayX) # join it


    or as others have mentioned



    print(",".join(sorted(word)))





    share|improve this answer































      0














      You want to use the string.join() function.



      alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
      ','.join(alphabet)


      There's really no need to anything to make the string into a list, join will iterate over it quite happily. Tried on python 2.7 and 3.6






      share|improve this answer

































        -2














        Doing it your self



        The trick is in the algorithm you use.



        You want to add a comma and a space, after each field, except the last. But it is hard to know which is the last, until it is too late.



        It would be much easier if you could make the first field the special case, as this is mach easier to predict.



        Therefore transform the algorithm to: Add a comma and a space, before each field, except the first. This produces the same output, but is a much simpler algorithm.



        Use a library



        Using a library is always preferable (unless doing it just for the practice).
        python has the join method. See other answers.






        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%2f53342836%2fhow-can-i-delete-comma-at-the-end-of-the-output-in-python%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Python strings have a join() function:



          ls = ['a','b','c']
          print(",".join(ls)) # prints "a,b,c"


          Python also has what is called a 'list comprehension', that you can use like so:



          alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
          word=str(input())
          matches = [l for l in word if l in alphabet]
          print(",".join(sorted(matches)))


          All the list comprehension does is put l in the list if it is in alphabet. All the candidate ls are taken from the word variable.



          sorted is a function that will do a simple sort (though more complex sorts are possible).



          Finally; here are a few other fun options that all result in "a,b,c,d":



          "a,b,c,d,"[:-1] . # list-slice
          "a,b,c,d,".strip(",") . # String strip





          share|improve this answer






























            2














            Python strings have a join() function:



            ls = ['a','b','c']
            print(",".join(ls)) # prints "a,b,c"


            Python also has what is called a 'list comprehension', that you can use like so:



            alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
            word=str(input())
            matches = [l for l in word if l in alphabet]
            print(",".join(sorted(matches)))


            All the list comprehension does is put l in the list if it is in alphabet. All the candidate ls are taken from the word variable.



            sorted is a function that will do a simple sort (though more complex sorts are possible).



            Finally; here are a few other fun options that all result in "a,b,c,d":



            "a,b,c,d,"[:-1] . # list-slice
            "a,b,c,d,".strip(",") . # String strip





            share|improve this answer




























              2












              2








              2







              Python strings have a join() function:



              ls = ['a','b','c']
              print(",".join(ls)) # prints "a,b,c"


              Python also has what is called a 'list comprehension', that you can use like so:



              alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
              word=str(input())
              matches = [l for l in word if l in alphabet]
              print(",".join(sorted(matches)))


              All the list comprehension does is put l in the list if it is in alphabet. All the candidate ls are taken from the word variable.



              sorted is a function that will do a simple sort (though more complex sorts are possible).



              Finally; here are a few other fun options that all result in "a,b,c,d":



              "a,b,c,d,"[:-1] . # list-slice
              "a,b,c,d,".strip(",") . # String strip





              share|improve this answer















              Python strings have a join() function:



              ls = ['a','b','c']
              print(",".join(ls)) # prints "a,b,c"


              Python also has what is called a 'list comprehension', that you can use like so:



              alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
              word=str(input())
              matches = [l for l in word if l in alphabet]
              print(",".join(sorted(matches)))


              All the list comprehension does is put l in the list if it is in alphabet. All the candidate ls are taken from the word variable.



              sorted is a function that will do a simple sort (though more complex sorts are possible).



              Finally; here are a few other fun options that all result in "a,b,c,d":



              "a,b,c,d,"[:-1] . # list-slice
              "a,b,c,d,".strip(",") . # String strip






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 16 '18 at 18:15

























              answered Nov 16 '18 at 17:42









              Nathaniel FordNathaniel Ford

              13.8k155676




              13.8k155676

























                  2














                  you store it in an array and then print it at the end



                  alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
                  word=str(input())
                  matches =
                  for i in alphabet:
                  for j in word:
                  if i==j:
                  matches.append("'{i}',".format(i=i))
                  #now that matches has all our matches
                  print(",".join(arrayX) # join it


                  or as others have mentioned



                  print(",".join(sorted(word)))





                  share|improve this answer




























                    2














                    you store it in an array and then print it at the end



                    alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
                    word=str(input())
                    matches =
                    for i in alphabet:
                    for j in word:
                    if i==j:
                    matches.append("'{i}',".format(i=i))
                    #now that matches has all our matches
                    print(",".join(arrayX) # join it


                    or as others have mentioned



                    print(",".join(sorted(word)))





                    share|improve this answer


























                      2












                      2








                      2







                      you store it in an array and then print it at the end



                      alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
                      word=str(input())
                      matches =
                      for i in alphabet:
                      for j in word:
                      if i==j:
                      matches.append("'{i}',".format(i=i))
                      #now that matches has all our matches
                      print(",".join(arrayX) # join it


                      or as others have mentioned



                      print(",".join(sorted(word)))





                      share|improve this answer













                      you store it in an array and then print it at the end



                      alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
                      word=str(input())
                      matches =
                      for i in alphabet:
                      for j in word:
                      if i==j:
                      matches.append("'{i}',".format(i=i))
                      #now that matches has all our matches
                      print(",".join(arrayX) # join it


                      or as others have mentioned



                      print(",".join(sorted(word)))






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 17:42









                      Joran BeasleyJoran Beasley

                      74.3k684122




                      74.3k684122























                          0














                          You want to use the string.join() function.



                          alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
                          ','.join(alphabet)


                          There's really no need to anything to make the string into a list, join will iterate over it quite happily. Tried on python 2.7 and 3.6






                          share|improve this answer






























                            0














                            You want to use the string.join() function.



                            alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
                            ','.join(alphabet)


                            There's really no need to anything to make the string into a list, join will iterate over it quite happily. Tried on python 2.7 and 3.6






                            share|improve this answer




























                              0












                              0








                              0







                              You want to use the string.join() function.



                              alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
                              ','.join(alphabet)


                              There's really no need to anything to make the string into a list, join will iterate over it quite happily. Tried on python 2.7 and 3.6






                              share|improve this answer















                              You want to use the string.join() function.



                              alphabet='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
                              ','.join(alphabet)


                              There's really no need to anything to make the string into a list, join will iterate over it quite happily. Tried on python 2.7 and 3.6







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 16 '18 at 17:51

























                              answered Nov 16 '18 at 17:43









                              Q-lifeQ-life

                              666




                              666























                                  -2














                                  Doing it your self



                                  The trick is in the algorithm you use.



                                  You want to add a comma and a space, after each field, except the last. But it is hard to know which is the last, until it is too late.



                                  It would be much easier if you could make the first field the special case, as this is mach easier to predict.



                                  Therefore transform the algorithm to: Add a comma and a space, before each field, except the first. This produces the same output, but is a much simpler algorithm.



                                  Use a library



                                  Using a library is always preferable (unless doing it just for the practice).
                                  python has the join method. See other answers.






                                  share|improve this answer




























                                    -2














                                    Doing it your self



                                    The trick is in the algorithm you use.



                                    You want to add a comma and a space, after each field, except the last. But it is hard to know which is the last, until it is too late.



                                    It would be much easier if you could make the first field the special case, as this is mach easier to predict.



                                    Therefore transform the algorithm to: Add a comma and a space, before each field, except the first. This produces the same output, but is a much simpler algorithm.



                                    Use a library



                                    Using a library is always preferable (unless doing it just for the practice).
                                    python has the join method. See other answers.






                                    share|improve this answer


























                                      -2












                                      -2








                                      -2







                                      Doing it your self



                                      The trick is in the algorithm you use.



                                      You want to add a comma and a space, after each field, except the last. But it is hard to know which is the last, until it is too late.



                                      It would be much easier if you could make the first field the special case, as this is mach easier to predict.



                                      Therefore transform the algorithm to: Add a comma and a space, before each field, except the first. This produces the same output, but is a much simpler algorithm.



                                      Use a library



                                      Using a library is always preferable (unless doing it just for the practice).
                                      python has the join method. See other answers.






                                      share|improve this answer













                                      Doing it your self



                                      The trick is in the algorithm you use.



                                      You want to add a comma and a space, after each field, except the last. But it is hard to know which is the last, until it is too late.



                                      It would be much easier if you could make the first field the special case, as this is mach easier to predict.



                                      Therefore transform the algorithm to: Add a comma and a space, before each field, except the first. This produces the same output, but is a much simpler algorithm.



                                      Use a library



                                      Using a library is always preferable (unless doing it just for the practice).
                                      python has the join method. See other answers.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 16 '18 at 17:53









                                      ctrl-alt-delorctrl-alt-delor

                                      4,33832644




                                      4,33832644






























                                          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%2f53342836%2fhow-can-i-delete-comma-at-the-end-of-the-output-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

                                          Xamarin.iOS Cant Deploy on Iphone

                                          Glorious Revolution

                                          Dulmage-Mendelsohn matrix decomposition in Python