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?
python python-3.x
New contributor
marked as duplicate by jpp
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.
add a comment |
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?
python python-3.x
New contributor
marked as duplicate by jpp
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.
add a comment |
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?
python python-3.x
New contributor
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
python python-3.x
New contributor
New contributor
edited yesterday
Benyamin Jafari
2,33531732
2,33531732
New contributor
asked 2 days ago
Artjom Vartanyan
12
12
New contributor
New contributor
marked as duplicate by jpp
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
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.
add a comment |
add a comment |
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))
Thank you!!!!!!
– Artjom Vartanyan
yesterday
add a comment |
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
The', '.join(dividers)
will not work if it is a list of integers.
– ritlew
2 days ago
add a comment |
up vote
0
down vote
print(i),
(with the comma) Should do the job.
New contributor
Since this is Python 3 it should beprint(i),
– Karl
2 days ago
@AmarNabhani Didn't work!
– Benyamin Jafari
yesterday
add a comment |
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))
Thank you!!!!!!
– Artjom Vartanyan
yesterday
add a comment |
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))
Thank you!!!!!!
– Artjom Vartanyan
yesterday
add a comment |
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))
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))
answered 2 days ago
ritlew
843411
843411
Thank you!!!!!!
– Artjom Vartanyan
yesterday
add a comment |
Thank you!!!!!!
– Artjom Vartanyan
yesterday
Thank you!!!!!!
– Artjom Vartanyan
yesterday
Thank you!!!!!!
– Artjom Vartanyan
yesterday
add a comment |
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
The', '.join(dividers)
will not work if it is a list of integers.
– ritlew
2 days ago
add a comment |
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
The', '.join(dividers)
will not work if it is a list of integers.
– ritlew
2 days ago
add a comment |
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
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
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
add a comment |
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
add a comment |
up vote
0
down vote
print(i),
(with the comma) Should do the job.
New contributor
Since this is Python 3 it should beprint(i),
– Karl
2 days ago
@AmarNabhani Didn't work!
– Benyamin Jafari
yesterday
add a comment |
up vote
0
down vote
print(i),
(with the comma) Should do the job.
New contributor
Since this is Python 3 it should beprint(i),
– Karl
2 days ago
@AmarNabhani Didn't work!
– Benyamin Jafari
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
print(i),
(with the comma) Should do the job.
New contributor
print(i),
(with the comma) Should do the job.
New contributor
edited yesterday
Benyamin Jafari
2,33531732
2,33531732
New contributor
answered 2 days ago
Amar Nabhani
1
1
New contributor
New contributor
Since this is Python 3 it should beprint(i),
– Karl
2 days ago
@AmarNabhani Didn't work!
– Benyamin Jafari
yesterday
add a comment |
Since this is Python 3 it should beprint(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
add a comment |