This article discusses how to represent a tab character in Python and some used cases.
The Tab Character in Python
The tab character in Python is represented by the escape sequence “\t”, as shown below.
1 2 3 |
# Escape sequence \t represents the tab character. print("Hello,\t Smith!") print("Hello, World. This\t is\t Smith with reg\t number\t 2719") |
Other ways to represent tab characters in Python include:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# chr(9) represents tab character print(f"Hello,{chr(9)} Smith!") # Unicode characters identified by name - represents tab character. print("Hello, \N{tab} Smith!") print("Hello, \N{HT} Smith!") print("Hello, \N{HT} Smith!") print("Hello, \N{CHARACTER TABULATION} Smith!") print("Hello, \N{HORIZONTAL TABULATION} Smith!") # A two-digit hexadecimal representation of a tab. print("Hello, \x09 Smith!") # 16-bit hexadecimal sequence for the tab character. print("Hello, \u0009 Smith!") # 32-bit hexadecimal sequence for the tab character. print("Hello, \U00000009 Smith!") |
Now, we can discuss some used cases for a tab in Python. We will use the “\t” in most of the coming examples, but you can use any character sequence given above.
Used Cases for the Tab Character in Python
There are several cases you can use the tab in Python. These include:
Example 1: Using a tab within a string
Anytime the Python interpreter finds the sequence “\t”, it inserts a tab character at that point. Here is an example.
1 |
print("Keep your\t bro\wser\t running.") |
Example 2: Using tab with string formatting
You can also pass a tab character into a string through f-string formatting (available on Python 3.6 and later) or string.format() function.
1 2 |
# Using <str>.format() function print("Keep{} your bro{}wser running.".format("\t", "\t")) |
For f-string, we cannot pass the “\t” character directly because f-string formatting does not accept any backslash within the placeholder(s). For example,
1 |
print(f"Keep{\t} your bro{\t}wser running.") |
Output:
SyntaxError: f-string expression part cannot include a backslash
You can navigate that problem by assigning the “\t” character into a variable and then using it in the placeholder. That is,
1 2 |
tab = "\t" print(f"Keep{tab} your bro{tab}wser running.") |
Alternatively, as shown below, you can use chr(9) as a tab character on the placeholders.
1 |
print(f"Keep{chr(9)} your bro{chr(9)}wser running.") |
You can also concatenate a tab character with other strings, as shown in the example below.
1 2 |
str1 = "Hello" + "\t" + "World!" print(str1) |
Example 3: Using a tab to join items of an object
We can join elements of an iterable using the “\t” character using the join() function, as shown below.
1 2 3 4 |
# Join elements of a list with a tab character. lst1 = ["San Diego", "Los Angeles", "San Francisco", "Michigan", "New York"] result = "\t".join(lst1) print(result) |
Example 4: Using a tab to separate items in the print statement
The print statement contains the sep argument that is used to separate the values in the output. The default value for “sep” is a space character. You can use the tab character by setting it to the “\t” sequence.
1 |
print("Apples", "Mangoes", "Oranges", sep= "\t") |
Conclusion
The “\t” character sequence is Python’s commonly used tab character. In this article, we discussed more character sequences you could use to represent a tab and some widely used cases for tab characters.