Printing a variable with n
Question:
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
Answers:
The easiest way to do this is to use an f-string to substitute variables, and triple-quotes to allow newlines in the string.
variable = f""" 1 2 3
A{A1} ---{A2}---{A3}
| | /|
| | / |
| |/ |
B {B1}---{B2}---{B3}
| /| |
| / | |
|/ | |
G {C1}---{C2}---{C3}"""
print(variable)
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)
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
The easiest way to do this is to use an f-string to substitute variables, and triple-quotes to allow newlines in the string.
variable = f""" 1 2 3
A{A1} ---{A2}---{A3}
| | /|
| | / |
| |/ |
B {B1}---{B2}---{B3}
| /| |
| / | |
|/ | |
G {C1}---{C2}---{C3}"""
print(variable)
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)