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.

Answer from Simone on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-print-a-tab-in-python-enhancing-text-formatting
How to Print a Tab in Python - GeeksforGeeks
July 23, 2025 - The easiest and most common way to print a tab space is by using the special character \t. This represents a tab space in Python.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 486422 › changing-all-tabs-to-spaces-line-by-line
python - Changing all tabs to spaces line by line | DaniWeb
What I'm trying to get it to do is when the function is called it takes the input file and converts all tabs into 4 spaces. Here's the form of the input and what I have of my tab_to_space function so far.
Discussions

How do I write a "tab" in Python? - Stack Overflow
Let's say I have a file. How do I write "hello" TAB "alex"? More on stackoverflow.com
🌐 stackoverflow.com
python - Modify the width of a tab in a format string - Stack Overflow
Is it possible to redefine the space width of a tab when printing a \t character in python? More on stackoverflow.com
🌐 stackoverflow.com
python - Printing with tab separation instead of spaces - Stack Overflow
Say I want to print like this: print 1,"hello", 2, "fart" but with tabs instead of spaces, what is the most pythonic way of doing this, in python 2? It's a really silly question but I couldn't se... More on stackoverflow.com
🌐 stackoverflow.com
For code indentation, is a single tab equivalent to 2 spaces or 4 spaces?
Question In Python, is a single tab equivalent to 2 spaces or 4 spaces, and if so can they be mixed? Answer In most code editors, tabs are not the same as 2 spaces or 4 spaces by default. A tab is stored differently than spaces in the code. Tabs can be seen as a big “jump” in the text, ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
23
July 19, 2018
🌐
LearnPython.com
learnpython.com › blog › indentation-python
What 'inconsistent use of tabs and spaces in indentation' Means in Python and How to Fix It | LearnPython.com
January 30, 2023 - To avoid this problem and remain consistent with the PEP 8 style guide, it’s also a good idea to configure your IDE to always insert 4 spaces when you hit the tab key. Configuring this is dependent on which IDE you use. For those of you not familiar with which IDEs are available, check out our article 4 Best Python IDE and Code Editors.
🌐
Renan Moura
renanmf.com › início › expandtabs(): set the number of spaces for a tab in a string in python
expandtabs(): set the number of spaces for a tab in a string in Python
March 21, 2021 - In this example the tabsize is 4, which gives us 3 spaces after the char ‘B’. my_string = 'B\tR' print(my_string.expandtabs(4)) #output: B R
🌐
Script Everything
scripteverything.com › posts › how to print a tab in python
How To Print A Tab In Python | Script Everything
November 7, 2021 - To print the tab character, use the abbreviated shorthand method of '\t' or chr(9) if using backslashes in your context will not work. Be mindful that the tab space is one of the whitespace characters, meaning if a tab is found at the beginning or trailing end of a string, it will be removed ...
Top answer
1 of 7
211

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.

2 of 7
47

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.

Find elsewhere
🌐
Programiz
programiz.com › python-programming › methods › string › expandtabs
Python String expandtabs()
Original String: xyz 12345 abc Tabsize 2: xyz 12345 abc Tabsize 3: xyz 12345 abc Tabsize 4: xyz 12345 abc Tabsize 5: xyz 12345 abc Tabsize 6: xyz 12345 abc · The default tabsize is 8. The tab stops are 8, 16 and so on. Hence, there is 5 spaces after 'xyz' and 3 after '12345' when you print the ...
🌐
Trey Hunner
treyhunner.com › tabs-and-spaces
Mixing Tabs & Spaces
Note that on the right, the `if` statement line is indented with 4 spaces and 1 tab, but Python sees this as equivalent to 8 spaces. There is a tab stop every 8 characters in Python. Tab *characters* only **fill the space needed** to reach the **next tab stop**. def progressively_more_spaces(): print("let's add one more space to the beginning...") print("of each line.") print("The effective indentation is the same") print("even though we're adding more spaces.") print("Each tab represents only the spaces needed") print("to get to the next 8 character tab stop.") if "your editor doesn't use 8 c
🌐
sebhastian
sebhastian.com › python-print-tab-spaces
How to print a tab in Python | sebhastian
January 29, 2023 - To print a tab in a Python string, you need to add the \t escape sequence in the middle of each word in your string. ... You don’t need to add any white spaces between the words you want to separate using a tab.
🌐
Python Forum
python-forum.io › thread-20713.html
Tab character in a string prints 8 spaces
Hi, I'm starting an online Python class and have been using Python 3 latest with IDLE. I open Python first then open IDLE next to it. Well, I got to the section using the newline and tab characters when printing strings. Then noticed something pecu...
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
For code indentation, is a single tab equivalent to 2 spaces or 4 spaces? - Python FAQ - Codecademy Forums
July 19, 2018 - Question In Python, is a single tab equivalent to 2 spaces or 4 spaces, and if so can they be mixed? Answer In most code editors, tabs are not the same as 2 spaces or 4 spaces by default. A tab is stored differently than spaces in the code. Tabs can be seen as a big “jump” in the text, while spaces are always 1 space each.
🌐
Vultr Docs
docs.vultr.com › python › standard library › str › expandtabs()
Python str expandtabs() - Set Tab Size
December 11, 2024 - By calculating the maximum length of the first column dynamically and setting the tab size accordingly, the expandtabs() method ensures that the second column is perfectly aligned across all items.
🌐
Medium
medium.com › python-basics-beyond › how-to-print-a-tab-in-python-a6c7df39c2ca
How to Print a Tab in Python. Learn different methods to print tab… | by A. S. M. Muhiminul Hasan | Python Basics & Beyond | Medium
December 3, 2024 - # Using f-strings with tabs name = "Bob" age = 35 print(f"{name}\t{age}") # Using format() method print("{}\t{}".format(name… ... Your go-to resource for Python programming insights, tips, and real-world applications.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-print-tab
How to print a Tab in Python | bobbyhadz
April 10, 2024 - The for loop iterates over the list and uses the addition (+) operator to add a tab after each item. The last step is to write a newline (\n) character to the file. Use the multiplication operator to print spaces.
🌐
Tutorial Reference
tutorialreference.com › python › examples › faq › python-how-to-print-tab
How to print Tab in Python | Tutorial Reference
March 29, 2025 - Using commas to print multiple values in the print() function will, by default, put a space between them. print('tutorial', 'reference', 'com') # Output: tutorial reference com · Control what is printed at the end using end argument: print('a', 'b', 'c', end='') # Output: 'a b c' Printing ...
🌐
Reddit
reddit.com › r/learnpython › tabs vs spaces (i don't get it)
r/learnpython on Reddit: Tabs vs Spaces (I don't get it)
April 14, 2018 -

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"!)

🌐
GitHub
github.com › googlecolab › colabtools › issues › 827
The Python tab setting should default to 4 spaces to respect PEP8 and the Google style guide · Issue #827 · googlecolab/colabtools
October 25, 2019 - Currently, when opening a Python notebook, the default tab size is 2. It can be changed with a setting (tools -> preference -> editor -> use 4 spaces), but the default should represent the most widely used workflow.
Author: googlecolab