1

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
asked Mar 9, 2018 at 5:29

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

print('1 2 3 4 5\n'*7)
General Grievance
5,12039 gold badges40 silver badges60 bronze badges
answered Mar 9, 2018 at 5:36

Comments

0
def main():
 print('1 2 3 4 5\n' * 7)
main()
answered Dec 12, 2023 at 17:43

1 Comment

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?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.