Print empty line?
Question:
I am following a beginners tutorial on Python, there is a small exercise where I have to add an extra function call and print a line between verses, this works fine if I print an empty line in between function calls but if I add an empty print line to the end of my happyBirthday()
I get an indent error, without the added print line all works fine though, any suggestions as to why?
Here is the code:
def happyBirthday(person):
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear " + person + ".")
print("Happy Birthday to you!")
print("n") #error line
happyBirthday('Emily')
happyBirthday('Andre')
happyBirthday('Maria')
Answers:
You will always only get an indent error if there is actually an indent error. Double check that your final line is indented the same was as the other lines — either with spaces or with tabs. Most likely, some of the lines had spaces (or tabs) and the other line had tabs (or spaces).
Trust in the error message — if it says something specific, assume it to be true and figure out why.
The two common to print a blank line in Python-
-
The old school way:
print "hellon"
-
Writing the word print alone would do that:
print "hello"
print
You can just do
print()
to get an empty line.
Don’t do
print("n")
on the last line. It will give you 2 empty lines.
Python 2.x:
Prints a newline
print
Python 3.x:
You must call the function
print()
Python’s print
function adds a newline character to its input. If you give it no input it will just print a newline character
print()
Will print an empty line. If you want to have an extra line after some text you’re printing, you can a newline to your text
my_str = "hello world"
print(my_str + "n")
If you’re doing this a lot, you can also tell print
to add 2 newlines instead of just one by changing the end=
parameter (by default end="n"
)
print("hello world", end="nn")
But you probably don’t need this last method, the two before are much clearer.
This is are other ways of printing empty lines in python
# using n after the string creates an empty line after this string is passed to the the terminal.
print("We need to put about", average_passengers_per_car, "in each car. n")
print("n") #prints 2 empty lines
print() #prints 1 empty line
I am following a beginners tutorial on Python, there is a small exercise where I have to add an extra function call and print a line between verses, this works fine if I print an empty line in between function calls but if I add an empty print line to the end of my happyBirthday()
I get an indent error, without the added print line all works fine though, any suggestions as to why?
Here is the code:
def happyBirthday(person):
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear " + person + ".")
print("Happy Birthday to you!")
print("n") #error line
happyBirthday('Emily')
happyBirthday('Andre')
happyBirthday('Maria')
You will always only get an indent error if there is actually an indent error. Double check that your final line is indented the same was as the other lines — either with spaces or with tabs. Most likely, some of the lines had spaces (or tabs) and the other line had tabs (or spaces).
Trust in the error message — if it says something specific, assume it to be true and figure out why.
The two common to print a blank line in Python-
-
The old school way:
print "hellon"
-
Writing the word print alone would do that:
print "hello"
print
You can just do
print()
to get an empty line.
Don’t do
print("n")
on the last line. It will give you 2 empty lines.
Python 2.x:
Prints a newline
print
Python 3.x:
You must call the function
print()
Python’s print
function adds a newline character to its input. If you give it no input it will just print a newline character
print()
Will print an empty line. If you want to have an extra line after some text you’re printing, you can a newline to your text
my_str = "hello world"
print(my_str + "n")
If you’re doing this a lot, you can also tell print
to add 2 newlines instead of just one by changing the end=
parameter (by default end="n"
)
print("hello world", end="nn")
But you probably don’t need this last method, the two before are much clearer.
This is are other ways of printing empty lines in python
# using n after the string creates an empty line after this string is passed to the the terminal.
print("We need to put about", average_passengers_per_car, "in each car. n")
print("n") #prints 2 empty lines
print() #prints 1 empty line