The most important ones are:
\n - newline character. You can't simply hit return while entering a string you want on several lines - even if you haven't closed quotes, Python interprets that as end of statement, and then complains about unclosed quotes.
>>> print ("Hello
SyntaxError: EOL while scanning string literal
>>> print ("Hello \n World")
Hello
World
>>>
\t - tab character indents following characters by multiples of 8, so for example
>>> for x in range(6):
y = round((1/(x+1)), 3)
print (y, "\t Hello World")
1.0 Hello World
0.5 Hello World
0.333 Hello World
0.25 Hello World
0.2 Hello World
0.167 Hello World
>>>
Some uses of the \ are to make Python treat the following character literally. For example, if you wanted to include an apostrophe in a string without signifying the end of the string:
>>> print ('Two\'s company, three\'s a crowd')Two's company, three's a crowdYou can also use it for double quotes:
>>> print ("\"Hello World!\" she said")Though to be honest there is another way of dealing with both of these - use the other style of quotes to enclose or delimit the string.
"Hello World!" she said
>>>
>>> print ("Two's company, three's a crowd")I learn something new every day with this blog (or so it seems). It turns out that with a backslash \ you can hit return in the middle of a string in code and it will carry on reading the string onto the next line rather than assuming the end of the statement. However, the line break is only in the code - it doesn't display when printed. For example:
Two's company, three's a crowd
>>> print ('"Hello World!" she said')
"Hello World!" she said
>>>
>>> print ("This string is just too long \But you could always combine this with \n for a newline both in code and printed:
for just one line")
This string is just too long for just one line
>>>
>>> print ("This string is just too long \n\And if you want to print a backslash as part of a string, you use double backslash \\:
for just one line")
This string is just too long
for just one line
>>>
>>> print ("Windows Path is C:\\Users\\John\\Dropbox\\Misc Programming\\Python\\python3")As you can see, because Windows file paths use backslash \ in a very different way, you need to be careful. If I jump ahead a bit and use the os module (for dealing with Operating Systems) with its getcwd() (get current working directory) function:
Windows Path is C:\Users\John\Dropbox\Misc Programming\Python\python3
>>>
>>> import osAs you can see, it prints the backslashes without problem, because the function has already included the escape \ character in the printed string with each printed \. However, when copying and pasting from something like Windows Explorer, you need to manually add in the escape \ or else Python will throw an error as it tries to interpret each \ as an escape character.
>>> print(os.getcwd())
C:\Users\John\AppData\Local\Programs\Python\Python36>>>
>>> print ("C:\Users\John\Dropbox\Misc Programming\Python\python3")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
>>>
No comments:
Post a Comment