The tab is a default space. There are many ways to print the tab in Python. In this post, you will some of the best ways to print a Tab in Python.
Methods to print a Tab in Python
Let’s explore all the methods to print a Tab in Python
Method 1: Use the \t character in the string
The first method to print the tab in Python is the use of\t character in the string. Below is an example of it.
website_name = 'Code\tThe\Best'
print(website_name )
Method 2: Use a tab when formatting a string
You can also use the tab while formatting a string literal. As you already know that formatting a string using the “f ” that allows you to use any variable inside the string. In this method, you will first define a variable that will contain the tab literal (\t) and use this variable inside the string.
tab = '\t'
string = f'code{tab}the{tab}best'
print(string)
Method 3: Using the tab with str.format()
In the second method, you have to use the “f” for formatting the string. Here you will use the str.format() function to include the tab variable inside the string.
Use the below lines of code to format the string with the tab literal.
string1 = "Code"
string2 = "The"
string 3= "Best
string = '{}\t{}\t{}'.format(string1,string2,string3)
print(string)
Conclusion
The above are the methods to print the tab to the screen. You can use any method of your choice. The best method I will suggest is method 3 which uses the formatting of the string.
Leave a Reply