here is the code:
print('Hello World\nIt\'s hot today')
okay I get the first slash is an escape sequence but whats the second slash for ?
here is the code:
print('Hello World\nIt\'s hot today')
okay I get the first slash is an escape sequence but whats the second slash for ?
print('Hello World\nIt\'s hot today')
The 2nd slash here, which appears before a '
character, allows you to use this character in a string which is scoped with it (otherwise, the Python interpreter has to conclude that this character is where the string ends).
Alternatively, you could scope this string with "
instead of '
, which would make that 2nd slash redundant:
print("Hello World\nIt's hot today")
The second \
is to escape '
. Without this, python would see
'Hello World\nIt's hot today'
Now, it would interpret 'Hello world\nIt'
as a string, because you ended it with '
! Then it does not know what to do with the rest of your code s hot today'
, resulting in an syntax error.
To avoid escaping '
, you could instead use "
:
print("Hello World\nIt's hot today")
The same goes for escaping "
. If you want to print a string He said "Hello, world!"
, then you would want either one of the following:
print("He said \"Hello, world!\"")
print('He said "Hello, world!"')