How do I add a new line to a string in python












-1














I have this code



def fullview():    
rows = 3
elems_in_row = 4
List = ['-'] * rows
for i in range(rows):
List[i] = ['-'] * elems_in_row
for i in List:
elements = ''.join(i)
fullview = str(rows)+ ':' + elements
rows -= 1
return fullview


When i add a line to fullview it removes the previous line added to it.
My desired result is:



fullview = """
3:----
2:----
1:----
"""


Im not sure how to add the new line correctly to the string since += dont work.










share|improve this question




















  • 2




    BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
    – PM 2Ring
    Nov 23 '18 at 12:53
















-1














I have this code



def fullview():    
rows = 3
elems_in_row = 4
List = ['-'] * rows
for i in range(rows):
List[i] = ['-'] * elems_in_row
for i in List:
elements = ''.join(i)
fullview = str(rows)+ ':' + elements
rows -= 1
return fullview


When i add a line to fullview it removes the previous line added to it.
My desired result is:



fullview = """
3:----
2:----
1:----
"""


Im not sure how to add the new line correctly to the string since += dont work.










share|improve this question




















  • 2




    BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
    – PM 2Ring
    Nov 23 '18 at 12:53














-1












-1








-1







I have this code



def fullview():    
rows = 3
elems_in_row = 4
List = ['-'] * rows
for i in range(rows):
List[i] = ['-'] * elems_in_row
for i in List:
elements = ''.join(i)
fullview = str(rows)+ ':' + elements
rows -= 1
return fullview


When i add a line to fullview it removes the previous line added to it.
My desired result is:



fullview = """
3:----
2:----
1:----
"""


Im not sure how to add the new line correctly to the string since += dont work.










share|improve this question















I have this code



def fullview():    
rows = 3
elems_in_row = 4
List = ['-'] * rows
for i in range(rows):
List[i] = ['-'] * elems_in_row
for i in List:
elements = ''.join(i)
fullview = str(rows)+ ':' + elements
rows -= 1
return fullview


When i add a line to fullview it removes the previous line added to it.
My desired result is:



fullview = """
3:----
2:----
1:----
"""


Im not sure how to add the new line correctly to the string since += dont work.







python string list printing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 13:03









khelwood

30.1k74162




30.1k74162










asked Nov 23 '18 at 12:39









monkey D

11




11








  • 2




    BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
    – PM 2Ring
    Nov 23 '18 at 12:53














  • 2




    BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
    – PM 2Ring
    Nov 23 '18 at 12:53








2




2




BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
– PM 2Ring
Nov 23 '18 at 12:53




BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
– PM 2Ring
Nov 23 '18 at 12:53












2 Answers
2






active

oldest

votes


















0














This can be done a single construction, instead of multiple steps:



result = 'n'.join(['{}:{}'.format(i, '-' * elements_in_row)
for i in range(1, rows + 1)][::-1])


One reason the original code did not work is that the line updating fullview did not take into account the previous state:



fullview = str(rows)+ ':' + elements





share|improve this answer





























    0














    Just use 'n'



    def fullview():
    rows = 3
    elems_in_row = 4
    fullview_str = ''
    for i in range(rows, 0, -1):
    fullview_str += "{}:{}n".format(i, '-' * elems_in_row)
    return fullview_str


    Example output:



    3:----
    2:----
    1:----





    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%2f53446896%2fhow-do-i-add-a-new-line-to-a-string-in-python%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














      This can be done a single construction, instead of multiple steps:



      result = 'n'.join(['{}:{}'.format(i, '-' * elements_in_row)
      for i in range(1, rows + 1)][::-1])


      One reason the original code did not work is that the line updating fullview did not take into account the previous state:



      fullview = str(rows)+ ':' + elements





      share|improve this answer


























        0














        This can be done a single construction, instead of multiple steps:



        result = 'n'.join(['{}:{}'.format(i, '-' * elements_in_row)
        for i in range(1, rows + 1)][::-1])


        One reason the original code did not work is that the line updating fullview did not take into account the previous state:



        fullview = str(rows)+ ':' + elements





        share|improve this answer
























          0












          0








          0






          This can be done a single construction, instead of multiple steps:



          result = 'n'.join(['{}:{}'.format(i, '-' * elements_in_row)
          for i in range(1, rows + 1)][::-1])


          One reason the original code did not work is that the line updating fullview did not take into account the previous state:



          fullview = str(rows)+ ':' + elements





          share|improve this answer












          This can be done a single construction, instead of multiple steps:



          result = 'n'.join(['{}:{}'.format(i, '-' * elements_in_row)
          for i in range(1, rows + 1)][::-1])


          One reason the original code did not work is that the line updating fullview did not take into account the previous state:



          fullview = str(rows)+ ':' + elements






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 12:46









          Elisha

          19.5k45069




          19.5k45069

























              0














              Just use 'n'



              def fullview():
              rows = 3
              elems_in_row = 4
              fullview_str = ''
              for i in range(rows, 0, -1):
              fullview_str += "{}:{}n".format(i, '-' * elems_in_row)
              return fullview_str


              Example output:



              3:----
              2:----
              1:----





              share|improve this answer


























                0














                Just use 'n'



                def fullview():
                rows = 3
                elems_in_row = 4
                fullview_str = ''
                for i in range(rows, 0, -1):
                fullview_str += "{}:{}n".format(i, '-' * elems_in_row)
                return fullview_str


                Example output:



                3:----
                2:----
                1:----





                share|improve this answer
























                  0












                  0








                  0






                  Just use 'n'



                  def fullview():
                  rows = 3
                  elems_in_row = 4
                  fullview_str = ''
                  for i in range(rows, 0, -1):
                  fullview_str += "{}:{}n".format(i, '-' * elems_in_row)
                  return fullview_str


                  Example output:



                  3:----
                  2:----
                  1:----





                  share|improve this answer












                  Just use 'n'



                  def fullview():
                  rows = 3
                  elems_in_row = 4
                  fullview_str = ''
                  for i in range(rows, 0, -1):
                  fullview_str += "{}:{}n".format(i, '-' * elems_in_row)
                  return fullview_str


                  Example output:



                  3:----
                  2:----
                  1:----






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 12:47









                  Ihor Voronin

                  1876




                  1876






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


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

                      But avoid



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

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


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





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


                      Please pay close attention to the following guidance:


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

                      But avoid



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

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


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




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446896%2fhow-do-i-add-a-new-line-to-a-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

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)