This is the code:
f = open(filename, 'w')
f.write("hello\talex")
The \t inside the string is the escape sequence for the horizontal tabulation.
How do I write a "tab" in Python? - Stack Overflow
python - Modify the width of a tab in a format string - Stack Overflow
python - Printing with tab separation instead of spaces - Stack Overflow
For code indentation, is a single tab equivalent to 2 spaces or 4 spaces?
This is the code:
f = open(filename, 'w')
f.write("hello\talex")
The \t inside the string is the escape sequence for the horizontal tabulation.
The Python reference manual includes several string literals that can be used in a string. These special sequences of characters are replaced by the intended meaning of the escape sequence.
Here is a table of some of the more useful escape sequences and a description of the output from them.
Escape Sequence Meaning
\t Tab
\\ Inserts a back slash (\)
\' Inserts a single quote (')
\" Inserts a double quote (")
\n Inserts a ASCII Linefeed (a new line)
Basic Example
If i wanted to print some data points separated by a tab space I could print this string.
DataString = "0\t12\t24"
print (DataString)
Returns
0 12 24
Example for Lists
Here is another example where we are printing the items of list and we want to sperate the items by a TAB.
DataPoints = [0,12,24]
print (str(DataPoints[0]) + "\t" + str(DataPoints[1]) + "\t" + str(DataPoints[2]))
Returns
0 12 24
Raw Strings
Note that raw strings (a string which include a prefix "r"), string literals will be ignored. This allows these special sequences of characters to be included in strings without being changed.
DataString = r"0\t12\t24"
print (DataString)
Returns
0\t12\t24
Which maybe an undesired output
String Lengths
It should also be noted that string literals are only one character in length.
DataString = "0\t12\t24"
print (len(DataString))
Returns
7
The raw string has a length of 9.
Hey Guys,
I've been following a Guide to python where they told us to use Tabs but then they sent us to the PEP8 Style Guide where it states:
"Spaces are the preferred indentation method."
Now, after I digging around a bit I found that you should use 4 spaces to indent code, sooo...
Why not just use Tab instead of hitting the space bar 4 times?
I'm guessing this is a hot topic, but I just wanted to make sure it's ok with going with Tabs over spaces (since it's the same lenght!). (I'm also a fan of Silicon Valley and as Richard Hendriks says: "2 Tabs save you 8 spaces"!)
A tab is an actual character “\t”. It’s usually represented as a space of length 4, but is still a single distinct character. 4 spaces is by far the preferred standard, but no one actually types 4 spaces each time, all but the most extremely basic text editors have a setting to convert tabs to spaces. This is what I and most people would suggest.
The style guide says:
Spaces are the preferred indentation method
because there can be a problem if you mix tab and space characters. Different editors/IDEs can interpret a mixture of tabs and spaces in different ways, leading to different indentations. I personally have seen a recent case where a user was getting an indentation error from python but the editor he was using showed good code. Using another editor or simply listing the code on the screen showed the bad indentation.
The cause of this difference in interpretation probably goes back to when computers took over from typewriters, but the result is we have different bits of software interpreting mixed tabs and spaces in different ways. The simplest way out of the problem is to not mix tabs and spaces. So use an editor that inserts the correct number of spaces when you press the TAB key, deletes the expected number of spaces when you press the DEL key, and NEVER writes a TAB character ('\t') to your source file on disk.
print(“John is hunger”, food, “is his favorite.”)
-how would I start a new line and indent at food?