How to print a collection of characters next to each other [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • How to print without newline or space?

    26 answers




I have this code where given an integer n, I want to print out all integers in the interval [1→n] that divide n, separated with spaces. I wrote this code:



n = int(input('Enter number:'))

for i in range(1, n+1):
if (n%i==0):
print (i)


I get this as the answer:




Enter number:8

1

2

4

8


But I want my answer next to each other, separated using spaces (so: 1 2 4 8). How do I do this?










share|improve this question









New contributor




Artjom Vartanyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    0
    down vote

    favorite













    This question already has an answer here:




    • How to print without newline or space?

      26 answers




    I have this code where given an integer n, I want to print out all integers in the interval [1→n] that divide n, separated with spaces. I wrote this code:



    n = int(input('Enter number:'))

    for i in range(1, n+1):
    if (n%i==0):
    print (i)


    I get this as the answer:




    Enter number:8

    1

    2

    4

    8


    But I want my answer next to each other, separated using spaces (so: 1 2 4 8). How do I do this?










    share|improve this question









    New contributor




    Artjom Vartanyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.











    marked as duplicate by jpp python
    Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    yesterday


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:




      • How to print without newline or space?

        26 answers




      I have this code where given an integer n, I want to print out all integers in the interval [1→n] that divide n, separated with spaces. I wrote this code:



      n = int(input('Enter number:'))

      for i in range(1, n+1):
      if (n%i==0):
      print (i)


      I get this as the answer:




      Enter number:8

      1

      2

      4

      8


      But I want my answer next to each other, separated using spaces (so: 1 2 4 8). How do I do this?










      share|improve this question









      New contributor




      Artjom Vartanyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      This question already has an answer here:




      • How to print without newline or space?

        26 answers




      I have this code where given an integer n, I want to print out all integers in the interval [1→n] that divide n, separated with spaces. I wrote this code:



      n = int(input('Enter number:'))

      for i in range(1, n+1):
      if (n%i==0):
      print (i)


      I get this as the answer:




      Enter number:8

      1

      2

      4

      8


      But I want my answer next to each other, separated using spaces (so: 1 2 4 8). How do I do this?





      This question already has an answer here:




      • How to print without newline or space?

        26 answers








      python python-3.x






      share|improve this question









      New contributor




      Artjom Vartanyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Artjom Vartanyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited yesterday









      Benyamin Jafari

      2,33531732




      2,33531732






      New contributor




      Artjom Vartanyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      Artjom Vartanyan

      12




      12




      New contributor




      Artjom Vartanyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Artjom Vartanyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Artjom Vartanyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      marked as duplicate by jpp python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      yesterday


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by jpp python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      yesterday


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote













          Instead of:



          print(i)


          You should put:



          print(i, end=" ")


          This will change the end of line string from "n" to " ". This will give you the desired output.





          Another method would be to build a list of results and print it out at the end:



          n = int(input('Enter number:'))
          final_results = list()

          for i in range(1, n+1):
          if (n%i==0):
          final_results.append(str(i))

          print(" ".join(final_results))





          share|improve this answer





















          • Thank you!!!!!!
            – Artjom Vartanyan
            yesterday


















          up vote
          0
          down vote













          I would suggest accumulating all intermediate results, and only when the computation is done, print it.



          n = int(input('Enter number:'))
          dividers =
          for i in range(1, n+1):
          if (n%i==0):
          dividers.append(i)
          print(dividers)


          If you want to print them with nice comma separation, you can do something like this:



          print(', '.join(str(divider) for divider in dividers))


          Benefits



          First, this reduces the number of calls to wherever you are printing to (by default, this is stdout)



          Second, the code becomes more readable and easier to adjust and expand later-on (for example, if you later decide that you want to pass those dividers onto another function)



          Edit: adjusted the join operation per ritlew's comment






          share|improve this answer























          • The ', '.join(dividers) will not work if it is a list of integers.
            – ritlew
            2 days ago




















          up vote
          0
          down vote













          print(i), 


          (with the comma) Should do the job.






          share|improve this answer










          New contributor




          Amar Nabhani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Since this is Python 3 it should be print(i),
            – Karl
            2 days ago










          • @AmarNabhani Didn't work!
            – Benyamin Jafari
            yesterday


















          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote













          Instead of:



          print(i)


          You should put:



          print(i, end=" ")


          This will change the end of line string from "n" to " ". This will give you the desired output.





          Another method would be to build a list of results and print it out at the end:



          n = int(input('Enter number:'))
          final_results = list()

          for i in range(1, n+1):
          if (n%i==0):
          final_results.append(str(i))

          print(" ".join(final_results))





          share|improve this answer





















          • Thank you!!!!!!
            – Artjom Vartanyan
            yesterday















          up vote
          3
          down vote













          Instead of:



          print(i)


          You should put:



          print(i, end=" ")


          This will change the end of line string from "n" to " ". This will give you the desired output.





          Another method would be to build a list of results and print it out at the end:



          n = int(input('Enter number:'))
          final_results = list()

          for i in range(1, n+1):
          if (n%i==0):
          final_results.append(str(i))

          print(" ".join(final_results))





          share|improve this answer





















          • Thank you!!!!!!
            – Artjom Vartanyan
            yesterday













          up vote
          3
          down vote










          up vote
          3
          down vote









          Instead of:



          print(i)


          You should put:



          print(i, end=" ")


          This will change the end of line string from "n" to " ". This will give you the desired output.





          Another method would be to build a list of results and print it out at the end:



          n = int(input('Enter number:'))
          final_results = list()

          for i in range(1, n+1):
          if (n%i==0):
          final_results.append(str(i))

          print(" ".join(final_results))





          share|improve this answer












          Instead of:



          print(i)


          You should put:



          print(i, end=" ")


          This will change the end of line string from "n" to " ". This will give you the desired output.





          Another method would be to build a list of results and print it out at the end:



          n = int(input('Enter number:'))
          final_results = list()

          for i in range(1, n+1):
          if (n%i==0):
          final_results.append(str(i))

          print(" ".join(final_results))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          ritlew

          843411




          843411












          • Thank you!!!!!!
            – Artjom Vartanyan
            yesterday


















          • Thank you!!!!!!
            – Artjom Vartanyan
            yesterday
















          Thank you!!!!!!
          – Artjom Vartanyan
          yesterday




          Thank you!!!!!!
          – Artjom Vartanyan
          yesterday












          up vote
          0
          down vote













          I would suggest accumulating all intermediate results, and only when the computation is done, print it.



          n = int(input('Enter number:'))
          dividers =
          for i in range(1, n+1):
          if (n%i==0):
          dividers.append(i)
          print(dividers)


          If you want to print them with nice comma separation, you can do something like this:



          print(', '.join(str(divider) for divider in dividers))


          Benefits



          First, this reduces the number of calls to wherever you are printing to (by default, this is stdout)



          Second, the code becomes more readable and easier to adjust and expand later-on (for example, if you later decide that you want to pass those dividers onto another function)



          Edit: adjusted the join operation per ritlew's comment






          share|improve this answer























          • The ', '.join(dividers) will not work if it is a list of integers.
            – ritlew
            2 days ago

















          up vote
          0
          down vote













          I would suggest accumulating all intermediate results, and only when the computation is done, print it.



          n = int(input('Enter number:'))
          dividers =
          for i in range(1, n+1):
          if (n%i==0):
          dividers.append(i)
          print(dividers)


          If you want to print them with nice comma separation, you can do something like this:



          print(', '.join(str(divider) for divider in dividers))


          Benefits



          First, this reduces the number of calls to wherever you are printing to (by default, this is stdout)



          Second, the code becomes more readable and easier to adjust and expand later-on (for example, if you later decide that you want to pass those dividers onto another function)



          Edit: adjusted the join operation per ritlew's comment






          share|improve this answer























          • The ', '.join(dividers) will not work if it is a list of integers.
            – ritlew
            2 days ago















          up vote
          0
          down vote










          up vote
          0
          down vote









          I would suggest accumulating all intermediate results, and only when the computation is done, print it.



          n = int(input('Enter number:'))
          dividers =
          for i in range(1, n+1):
          if (n%i==0):
          dividers.append(i)
          print(dividers)


          If you want to print them with nice comma separation, you can do something like this:



          print(', '.join(str(divider) for divider in dividers))


          Benefits



          First, this reduces the number of calls to wherever you are printing to (by default, this is stdout)



          Second, the code becomes more readable and easier to adjust and expand later-on (for example, if you later decide that you want to pass those dividers onto another function)



          Edit: adjusted the join operation per ritlew's comment






          share|improve this answer














          I would suggest accumulating all intermediate results, and only when the computation is done, print it.



          n = int(input('Enter number:'))
          dividers =
          for i in range(1, n+1):
          if (n%i==0):
          dividers.append(i)
          print(dividers)


          If you want to print them with nice comma separation, you can do something like this:



          print(', '.join(str(divider) for divider in dividers))


          Benefits



          First, this reduces the number of calls to wherever you are printing to (by default, this is stdout)



          Second, the code becomes more readable and easier to adjust and expand later-on (for example, if you later decide that you want to pass those dividers onto another function)



          Edit: adjusted the join operation per ritlew's comment







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          Ori

          647715




          647715












          • The ', '.join(dividers) will not work if it is a list of integers.
            – ritlew
            2 days ago




















          • The ', '.join(dividers) will not work if it is a list of integers.
            – ritlew
            2 days ago


















          The ', '.join(dividers) will not work if it is a list of integers.
          – ritlew
          2 days ago






          The ', '.join(dividers) will not work if it is a list of integers.
          – ritlew
          2 days ago












          up vote
          0
          down vote













          print(i), 


          (with the comma) Should do the job.






          share|improve this answer










          New contributor




          Amar Nabhani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Since this is Python 3 it should be print(i),
            – Karl
            2 days ago










          • @AmarNabhani Didn't work!
            – Benyamin Jafari
            yesterday















          up vote
          0
          down vote













          print(i), 


          (with the comma) Should do the job.






          share|improve this answer










          New contributor




          Amar Nabhani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Since this is Python 3 it should be print(i),
            – Karl
            2 days ago










          • @AmarNabhani Didn't work!
            – Benyamin Jafari
            yesterday













          up vote
          0
          down vote










          up vote
          0
          down vote









          print(i), 


          (with the comma) Should do the job.






          share|improve this answer










          New contributor




          Amar Nabhani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          print(i), 


          (with the comma) Should do the job.







          share|improve this answer










          New contributor




          Amar Nabhani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer








          edited yesterday









          Benyamin Jafari

          2,33531732




          2,33531732






          New contributor




          Amar Nabhani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 2 days ago









          Amar Nabhani

          1




          1




          New contributor




          Amar Nabhani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Amar Nabhani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Amar Nabhani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • Since this is Python 3 it should be print(i),
            – Karl
            2 days ago










          • @AmarNabhani Didn't work!
            – Benyamin Jafari
            yesterday


















          • Since this is Python 3 it should be print(i),
            – Karl
            2 days ago










          • @AmarNabhani Didn't work!
            – Benyamin Jafari
            yesterday
















          Since this is Python 3 it should be print(i),
          – Karl
          2 days ago




          Since this is Python 3 it should be print(i),
          – Karl
          2 days ago












          @AmarNabhani Didn't work!
          – Benyamin Jafari
          yesterday




          @AmarNabhani Didn't work!
          – Benyamin Jafari
          yesterday



          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin