Hướng dẫn python print tab-separated
Show Problem Statement: How to separate the items of a list in Python using tab as the delimiter? Example: The following example demonstrates an example of the given problem statement. # Given list lst = ['100', '200', '300', '400', '500'] # Some way to print the list values separated by tab # Expected Output --> 100 200 300 400 500 The problem is self-explanatory. Hence, without wasting too much time, let’s dive into the various ways of solving this problem. Method 1: Using Escape Sequence \tIn Python, we can indicate multiple strings using the print statement. We have to just add a comma ‘,’ between them, and they will be printed with a space in the middle. Refer to the example below: name = "Fin" print(" Hello", name) # Hello Fin In Python strings, the backslash “\” is a unique character likewise called the escape character. It is utilized in addressing certain whitespace characters, e.g. “\t” is a tab and “\n” is a newline. Example: print("Hello \t Welcome to Finxter") # Hello Welcome to Finxter On the other hand, prefixing a unique character with backslash “\” transforms it into a standard character. This is also known as escaping. For instance, “\'” is the single quote character. Here’s a related question: “How to print quotation marks in Python?” We can use escaping in this case as shown below: print("I love \'Python\'") # I love 'Python' You can directly use the escape sequence “ Solution: # Given list lst = ['100', '200', '300', '400', '500'] print(lst[0]+"\t"+lst[1]+"\t"+lst[2]+"\t"+lst[3]+"\t"+lst[4]) Output:
Method 2: Assigning \t to SepThere are two little-used arguments of the print function in Python. The argument Example: a = 'hello' b = 'world' print(a, b, sep=' Python ', end='!') # hello Python world! We can use the escape sequence “\t” and assign it to the argument “sep” to print the values of a list separated by tabs in Python. Example: # Function that will separate the list by tab def list_tab(lst): print(*lst, sep='\t') # Given list lst = ['Python', 'list', 'separated', 'by', 'tabs'] # Calling the function list_tab(lst) Output:
What does *lst operator do? Method 3: Using Map and JoinBefore having a look at the solution, we need to understand a couple of built-in methods in Python.
Recommended Reads:
Thus, you can simply use the Example: lst = ['Python', 'list', 'separated', 'by', 'tabs'] print('\t'.join(map(str, lst))) Output:
Method 4: Using join() with List ComprehensionIn
case you are not a big fan of the Example: lst = ['Python', 'list', 'separated', 'by', 'tabs'] print('\t'.join([str(x) for x in lst])) # Python list separated by tabs List comprehension is a compact way of creating lists. The simple formula is [expression + context]. ConclusionIn this article, we discussed numerous ways of printing tab-separated values of a Python list. I hope it was helpful and it answered all your queries. For more solutions and discussions like this, please stay tuned and subscribe. Nerd HumorRecommended: Finxter Computer Science Academy
Join the PyCharm Masterclass now, and master PyCharm by tomorrow! I am a professional Python Blogger and Content creator. I have published numerous articles and created courses over a period of time. Presently I am working as a full-time freelancer and I have experience in domains like Python, AWS, DevOps, and Networking. You can contact me @: UpWork |