FAQ: Learn Python: Syntax - Print

This community-built FAQ covers the “Print” exercise from the lesson “Learn Python: Syntax”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Print

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: How can I ask the user for input?

2 posts were split to a new topic: How can I skip or jump to a new line while printing?

2 posts were split to a new topic: How can I use quotes inside of a string?

Why do we need to put up the input message in brackets?
what is the reason behind using () in the print statement?

That’s part of the syntax for the standard way to call a function:

function(arguments)

You’ll come across this with a bit more information as you progress through the lessons.

2 Likes

what if you need to show possession inside the string, for example, if i wanted to say, “This is Fred’s ball” how would i not incur a syntax error for the 's

In most cases you can use the escape character \ to include characters which would otherwise have a special meaning in string literals. For quotes you could rely on internal characters that are different from the outer quotes used to mark the string but be careful about overcomplicating the code since it can become very hard to read and understand:

print("Where's my porridge?")
print("'This porridge is too hot!', said the hairy bear.")
# triple quotes allow other options but can potentially introduce even more complexity.
print('''"Inconceivable!", exclaimed the museli, "I'll not tolerate such slander from a hairy bear."''')
2 Likes

when we type nothing inside () of print() it print nothing but there is something there which breaks the line so what is the default value of it?

You can check it the docs- https://docs.python.org/3/library/functions.html#print but there is a default argument to the end parameter of the print function which is end='\n' hence you get the new line. If you wanted to change it it’s just print(my_object_to_print, end='my new ending') which can be any valid string including empty ones e.g. ''.

1 Like

my input is:
print(1)
print()
print(2)
and the out put comes like below:
1

2
so my question is what is between 1 and 2? when
i’m typing nothing inside print()

Sorry I wasn’t clear before. Print basically adds a newline character '\n' by default.

This why your output for 1 and 2 are on new lines and why print without arguments creates a gap, it’s just a new line. If you changed the default of the end parameter in print you can alter this behaviour. For example-

print(1, end='')  # we have changed end from default '\n' to an empty string
print(end='')
print(2, end='')

would have an output of 12 which is just the characters 1 and 2 without the new lines. So what’s between 1 and 2 in your example is just a new line.

1 Like

i have a doubt in the lesson 2 of function which is " What is function?"

there is something like this:

def sing_song():
print(“You may say I’m a dreamer”)
print(“But I’m not the only one”)
print("I hope some day you’ll join us ")
print(“And the world will be as one”)

#call sing_song() below:

now i call the function by two ways
first directly
sing_song()

and second is by use of print()
print(sing_song())

so when I call the function directly (i.e. sing_song() )
the output is

You may say I’m a dreamer
But I’m’ not the only one
I hope some day you’ll join us
And the world will be as one

but when I’m uses second way (i.e. print(sing_song))
the output at this time is:

You may say I’m a dreamer
But I’m’ not the only one
I hope some day you’ll join us
And the world will be as one
None

so, MY QUESTION IS THAT WHEN I’M USING print() FOR CALLING THE sing_song()
THE OUTPUT IS DIFFERENT FROM WHEN I’M USING ONLY sing_song()

the difference is that an extra term “None” come at last when I’m using print()

so, why this “None” come?
is print(sing_song)) is same as sing_song()?

Most of the time you’ll be using return to return from a function. Functions whose sole purpose is to print are rare and you should probably make clear that is their exact purpose. A function without a valid return/yield statement will always return the None object.

You call the function in the same way both times- myfunction(). When you wrap that function call in print then the function will be evaluated first and only then print will act on the output or return of the function. Since nothing is returned it defaults to returning None and therefore None is printed to the screen.

You’ll see the strings from within the function printed before None because the function is executed first and during its execution it prints to your console.

print(sing_song) is different to sing_song() and would also be different to print(sing_song()).

2 Likes

means
in above function sing_song()

when I input like below:
print(print(print(print(sing_song()))))

the output will like below?

You may say I’m a dreamer
But I’m’ not the only one
I hope some day you’ll join us
And the world will be as one
None
None
None
None

now if i want this all four None side by side (in same line)
so how can I use ‘\n’ ?

shall you please write the correct sequence for it?

Have you tried nesting your print statements as per your code? You’ll soon see how the output appears but note that print itself acts as a function without a return. Please see a previous response for details of how to alter your print output to avoid the addition of a newline- FAQ: Learn Python: Syntax - Print

Why does this not print on a separate line after the backslash?

separated = “This string is broken up \
over multiple lines”

print(separated)

The single slash \ tells the code to continue on the next line. However, that does not make this a pre-formatted string. A pre-formatted string takes three quotation marks.

// Python ignores all non expressly printed characters in single quotes
separated = "First Line \ 
Second Line"
print(separated)
# First Line Second Line

// Solution 1: Add a new line
separated = "First Line \n Second Line"
print(separated)
# First Line 
# Second Line

// Solution 2: Use a string literal (it will try and maintain the text formatting)
separated = """
First line 
Second line
"""
print(separated)
# First Line
# Second Line

ok thanks very much. pls is there any codecademy learning app that i can pin start it in my machine so as to ease my getting into leasons, we always have network failure here so at times to get into leasons is difficult