Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing a variable with \n

So I’m doing an assignment for my university’s Python class and I’m trying to print a shape (A*,B* and C* are variables, if needed I’ll post the full code)

A1 = None
A2 = None
A3 = None
B1 = None
B2 = None
B3 = None
C1 = None
C2 = None
C3 = None
if not A1:
    A1 = " "
if not A2:
    A2 = " "
if not A3:
    A3 = " "
if not B1:
    B1 = " "
if not B2:
    B2 = " "
if not B3:
    B3 = " "
if not C1:
    C1 = " "
if not C2:
    C2 = " "
if not C3:
    C3 = " "
print("  1   2   3 "
      "\nA" + A1 + " ---" + A2 + "---" + A3,
      "\n  |\  |  /|"
      "\n  | \ | / |"
      "\n  |  \|/  |"
      "\nB " + B1 + "---" + B2 + "---" + B3,
      "\n  |  /|\  |"
      "\n  | / | \ |"
      "\n  |/  |  \|"
      "\nG " + C1 + "---" + C2 + "---" + C3)

What is the correct syntax for inserting the contents of the previous command into a variable? I tried just copying and pasting it into a variable called box but \n is detected as a character and not as newline

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just replace the commas in your print statement with + so that all the strings are concatenated together (rather than forming a tuple):

box = ("  1   2   3 "
      "\nA" + A1 + " ---" + A2 + "---" + A3 +
      "\n  |\  |  /|"
      "\n  | \ | / |"
      "\n  |  \|/  |"
      "\nB " + B1 + "---" + B2 + "---" + B3 +
      "\n  |  /|\  |"
      "\n  | / | \ |"
      "\n  |/  |  \|"
      "\nG " + C1 + "---" + C2 + "---" + C3)

print(box)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading