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."''')
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. ''.
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.
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()).
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
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