In most of the programming languages, the 1st program a student learns to write is the so-called “Hello World!” program.
print(“Hello World!”)
This one-line program will give an output (print on the screen) the message Hello World!.
The things that this simple program is showing us, are the following:
– to print something on the screen, the print statement should be used.
– the message that we want to be printed is written between quotation marks “”.
– what is with round brackets? The brackets are part of the print statement: print(). The message that needs to be printed must be inserted between the brackets.
Can ‘ be used instead of “?
Answer: Yes, Try it yourself.
Are there any benefits when using one or another?
Answer: Yes, and I will let you figure it out by writing the following examples.
print(‘Dog’s bone’)
^
SyntaxError: invalid character in identifier
print(“Using this ” mark here will give an error!”)
^
SyntaxError: invalid character in identifier
print(‘Here the sign ” will not give any error’)
print(‘Here the sign ” will not give any error’)
If our message contains ‘ we must enclose it between ” “. The same situation is also for the ” sign, it must be enclosed between ‘ ‘.
But what can we do if we really want to write a message that contains ‘ and it is enclosed between’ ‘?
Answer: Python will let us do this with the help of an escape character.
The escape character is a backslash () plus the character/sign that we want to be included in the message (character/sign that normally would give a syntax error or with the help of backslash, can have a different meaning ).
Now, let’s try again to write in the console the examples from above where Phyton complained about a syntax error, but this time using backslash.
print(“Using this \” mark here will give an error!”)
print(‘Dog\’s bone’)
See you next time.
Top comments (0)