In this video, Kathryn shows you how to format your text with Python's text wrap module. Learn how to dedent and indent your text, set how much space the text will occupy, and how to add a placeholder such as an ellipsis if the text is too long.
- [Instructor] Let's figure out how to format text with the Text Wrap Module, and in order to use this module we need to import it, and so we're going to go import textwrap, and we'll go ahead and print the text without any dedenting, and we'll learn what that means in a minute. So we'll go print(textwrap.fill(websiteText)), and so printing this out you'll see that when we printed it, it kept this beginning space that we had up here but it didn't keep our enters, meaning it broke our text at different places.
So it kind of edited it on its own and filled a certain particular space but still kept this beginning tab here. This is one way that you could display your text. Another way is to have dedenting, and what this is going to do is it's going to take away our beginning and later spaces and it's going to keep the enters that we had here, it's not going to reformat them. And so in order to create dedent text we'll go dedent_text = textwrap.dedent(websiteText).strip, and then we'll go ahead and print out this dedented text, dedent_text, run it, and we'll see that it takes away this beginning space but it keeps the enters or the words we entered on inside the body of the text.
This again is another way that you could display text with the Text Wrap Module. But we can also make it so our text fills only a certain portion of the page, say 30 columns or 80 columns, so let's go ahead and try this by going print(textwrap.fill). We'll put in the dedented text which is formatted appropriately, and then we'll add the number of columns that we want it to spread across. And you'll get a sense of how many columns equal how much space once you start messing with it, and so we'll put in 50 here and then we'll add another print statement here so that way it's separated from what we did before.
And here we can see it filling about 50 of the characters across the screen. Trying this with a different column number we could go print(textwrap.fill, again with our dedented text, and we'll have the width be 100 say, and it will fill it out across 100 characters even if those characters go off the screen as we see there. So it's really up to you as well as the size of the font, how much a certain text fills a certain space.
We can also control the indent on text and so we can go print(textwrap.fill. We'll again use this dedent_text that takes away that beginning space, and then we'll have initial_indent, and you could make this as big or as little as you want. We're going to make it three spaces here and then we can say subsequent_indent and have that equal nothing for now. Running this, and now we can see the text has this beginning indent.
Now we can also add a subsequent indent, make this one, two, three tabs worth. We go ahead and run it and it's going to go ahead and tab the subsequent lines by that much. The last thing that we can do with the Text Wrap Module is we can shorten text or abbreviate it in a certain way. We could have short = textwrap.shorten, so using the shorten method, write in some code here, "LinkedIn.com is great!".
We'll make the width, which is how many columns or how many characters that we're going to go until we replace the rest with the placeholder, and that is going to be 15 here, and then we're going to go ahead and make the placeholder, which is going to be what comes after our 15 columns or our 15 characters, and we'll make that just dot dot dot, and so we print this out. So we'll go ahead and run this, and we see that LinkedIn.com goes 15 columns, we only see that portion, and then we get the dot dot dot.
If you have a program where you need to format output text that goes to a file or straight to the user through the print function, this will be super helpful for you.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content