1
print("=" * 100 + '\n' + "Can I be more elegant?" + '\n' + "=" * 100)

Is there a better elegant to print this?

Output

========================

Can I be more elegant?

========================

1
  • 2
    print("{0}\nCan I be more elegant?\n{0}".format("=" * 100)) Commented Aug 29, 2017 at 8:29

2 Answers 2

1

If you're using 3.6 or later, you can use the cool new f-strings.

print(f"{'='*100}\nCan I be more elegant?\n{'='*100}")

Prior to that, you can use format.

print("{0}\nCan I be more elegant?\n{0}".format('='*100))
Sign up to request clarification or add additional context in comments.

Comments

0

Your first example was better. print("We have x = %i" %x) because you are working with a string object. It takes less memory than working with 2 string objects and an int.

Since you are asking with a python3 tag, here is a newer format for python string formatting using str.format

dic = { 'x': '1'} print("We have x = {x}".format(**dic))

or you can do this positionally:

print("The sum of 1 + 2 is {0}".format(1+2))

This also works with 2.6 and 2.7.

Comments

Your Answer

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.