I want to format the output from print function.
def main():
print('1 2 3 4 5'*7)
# Write code here
main()
Required Output:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Obtained Output:
1 2 3 4 51 2 3 4 51 2 3 4 51 2 3 4 51 2 3 4 51 2 3 4 51 2 3 4 5
How do I make print function in Python3 to perform this job?
Cœur
39k25 gold badges207 silver badges282 bronze badges
3 Answers 3
You can set the separator to be a linebreak.
print(*7*('1 2 3 4 5',), sep='\n')
Equivalently, you can add the linebreak at the end of the string and remove the end linebreak from print.
print(7*'1 2 3 4 5\n', end='')
answered Mar 9, 2018 at 5:39
Olivier Melançon
22.5k4 gold badges48 silver badges81 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Try this:
print('1 2 3 4 5\n'*7)
General Grievance
5,12039 gold badges40 silver badges60 bronze badges
Comments
def main():
print('1 2 3 4 5\n' * 7)
main()
1 Comment
Jeremy Caney
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?
lang-py