0
    print("\nINSTRUCTIONS~\nEnter:\n'c' to use a full fledged calculator,")
    print("'a' to add and subtract numbers " +
          "(faster than calculator if you just want to add and subtract),")
    print("'s' to find a number's square root,")
    print("'p' to see powers,")
    print("'n' to arrange numbers in ascending and descending order,")
    print("'f' to calculate factorials,\n'x' to exit the program,")

I currently put a "+" for sentences on the same line, but otherwise make another print statement, I would like to know which would be a better programming practice.

Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
Neel Kamath
  • 1,188
  • 16
  • 34
  • This would be the perfect use for a multi line string. – Morgan Thrapp May 24 '16 at 14:49
  • @MorganThrapp meaning + right? – Neel Kamath May 24 '16 at 15:00
  • Nope, http://stackoverflow.com/questions/2504411/proper-indentation-for-python-multiline-strings – Morgan Thrapp May 24 '16 at 15:01
  • You can use `""" your text"""` – qvpham May 24 '16 at 15:05
  • But this wouldn't be very readable if I had the print tabbed in like 4 times since I'd have to make the docstring to the utmost left to prevent it from showing the blank spaces from tabbing in the code on the screen. – Neel Kamath May 25 '16 at 02:41
  • 1
    @NeelKamath: Triple-quoted strings and docstrings are different concepts. A docstring is a string that appears as its own as the first statement of a function, class, or module, and is used as the `__doc__` attribute of the thing it documents. Whether a string uses triple quotes is unconnected to whether it's a docstring. – user2357112 May 25 '16 at 23:28

3 Answers3

3

To answer your question, they are essentially the same. It is really a matter of readability, and thus personal preference.

However, there is a more convenient way to do it, which is to use multi line strings. They are delimited by """, and are essentially strings that are capable of spanning multiple lines.

For example,

print("Hello,
world!")

would throw an error saying EOL while scanning string literal, whereas

print("""Hello,
world!""")

is fine and prints this:

Hello,
World!

as it is supposed to.


Clarification: It is not the same as using the line continuation character(\). That only goes to the next line without breaking the string, to help the programmer read their code. It does not include the newline.

This:

print("Hello,\
world!")

Is not the same as this:

print("""Hello,
world!""")

While it is true they are both valid, they have different results. The former would print:

Hello,world!

While the latter would print

Hello,
world!

EDIT: When using this for a docstring, there is the concern that indentation meant for human readability will interfere with it by adding in extra tabs. Example:

# Less easily readable
def test():
    """Docstring
Docstring"""
    pass

# More easily readable
def otherTest():
    """Docstring
    Docstring"""
    pass

The thing is that these two docstrings produce the exact same result. Python ignores the leading whitespace.


Source: Proper indentation for Python multiline strings

Community
  • 1
  • 1
Ecko
  • 1,030
  • 9
  • 30
2

In terms of speed they are equivalent.

text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'

size = 5


def test_multiple_prints():
    for _ in range(size):
        print(text)


def test_single_print():
    big_text = '\n'.join([text for _ in range(size)])
    print(big_text)


%timeit test_multiple_prints()
#1000 loops, best of 3: 702 µs per loop


%timeit test_single_print()
#1000 loops, best of 3: 703 µs per loop
Erotemic
  • 4,806
  • 4
  • 39
  • 80
2

Do whatever makes the program easiest to read and easiest to maintain.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685