Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Modern Python Standard Library Cookbook

You're reading from   Modern Python Standard Library Cookbook Over 100 recipes to fully leverage the features of the standard library in Python

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781788830829
Length 366 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
 Molina Molina
Author Profile Icon Molina
Molina
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
1. Containers and Data Structures 2. Text Management FREE CHAPTER 3. Command Line 4. Filesystem and Directories 5. Date and Time 6. Read/Write Data 7. Algorithms 8. Cryptography 9. Concurrency 10. Networking 11. Web Development 12. Multimedia 13. Graphical User Interfaces 14. Development Tools 1. Other Books You May Enjoy Index

Aligning text


When printing tabular data, it's usually very important to ensure that the text is properly aligned to a fixed length, no longer and no shorter than the space we reserved for our table cell.

If the text is too short, the next column might start too early; if it's too long, it might start too late. This leads to results like this:

col1 | col2-1
col1-2 | col2-2

Or this:

col1-000001 | col2-1
col1-2 | col2-2

Both of these are really hard to read and are far from showing a proper table.

Given a fixed column width (20 characters), we want our text to always be of that exact length so that it won't result in a misaligned table.

How to do it...

Here are the steps for this recipe:

  1. The textwrap module once combined with the features of the str object can help us achieve the expected result. First we need the content of the columns we want to print:
cols = ['hello world', 
        'this is a long text, maybe longer than expected, surely long enough', 
        'one more column']
  1. Then we need to fix...
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Modern Python Standard Library Cookbook
You have been reading a chapter from
Modern Python Standard Library Cookbook
Published in: Aug 2018
Publisher: Packt
ISBN-13: 9781788830829
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images
Modal Close icon
Modal Close icon