 Hello everybody and welcome back to another Python programming tutorial. I'm Root of the Null and today We're going to be looking at the text wrap module I've got the documentation already up if you need to find it You can find it online at docs.python.org and just search for the text wrap module You can see right up here now this text wrap module is super convenient because it is a built-in that kind of It comes along with with Python. It's part of the Python standard library So no matter what a system you're running on when no matter what platform whether or not it be Windows or Linux You will always have this module available to you. Now. It's new in version 2.3. You can see up here I'm running version 2.79 for Python. I think I'm actually on 2.77 on my system But whatever the case may be we can use this module now I've got my text editor open up already. I'm on Windows right now So I'm going to be using a different subang line than normal Python 2.7 Python.exe All right now the text wrap module itself is super cool and super convenient when it comes to working with text You can probably tell it it does text wrapping and filling hence it That's the big title up top here It provides two convenience functions wrap and fill as well as a text wrapper object or the class It really does all the work and a utility function called D-Dent in this tutorial I'm going to be showing you guys wrap and fill together because I kind of go hand-in-hand and The text wrapper object is really the behind-the-scenes thing that does all the stuff Utility function D-Dent is super cool and we'll get that one in another tutorial But if you're it says here if you're just wrapping you're filling one or two text strings The convenience function should be good enough Otherwise you should use an instance of the text wrapper class for efficiency and it gets on later to tell you about that The first two functions up here wrap and fill they take a single Optional sorry single argument the other one is optional being with here The single parameter and argument that it takes is a string You can tell that if you read here the description What this function will do rap anyway is it wraps the single paragraph in text being a string So that every line is at most with characters long it returns a list of the output lines without new lines and With by default is 70 like kind of a console or a terminal or a command line shell you'd be working in so that's a that's Hey, that's that's cool behavior. Let's check it out We're gonna want to import text wrap I'm gonna go ahead and create a new function over here It's just for I don't know good practice and Python and I'll run through the like the boilerplate code that actually makes our code run All right, so if we need a string to pass in I'm gonna go ahead and create mine I'll call mine long string because this is gonna have to be anyway a really really long string I'm happy To be making videos again. I only have a short Window of time to do it But I will when I can I can record in the library at school and only rarely in my room But right now I'm home for the holidays Merry Christmas everybody All right, that's cool. So we've got a really really long string and I give you a little bit of my background What's what's going on anyway? Let's check out this function. It's text wrap dot wrap We got our module text wrap wrap and we'll pass in our long string And I'll print out what this returns. We can check it out. I'm a sublime text All I'll do is press control B and check it out It returns a list for us. You can see the braces and the quotes here. This is a really really long string I'm happy to be making videos again I only have a short window of time and you can see there's these commas Representing that there are different items in the list It's separated the string entirely into pieces and kind of divided it by this this width of 70 by default We can check this out for a line. Actually, let's let's let's set this as a variable Let's call it wrap lines Now for line and wrap lines We can print out the length of the line along with the line. Let's check it out Now this is going to give us a nice display kind of like a paragraph. This is a really really long string I'm happy to be making videos again and all the text that we wanted 67 69 66 63 and 37 you can see it never surpasses 70 the documentation told us It will be at most with characters long But it's gonna stop as soon as it gets to something that it can't chop Correctly anyway, because you know if you're reading text if it just stops in the middle of the word You kind of have a hyphen to carry it on that's a little messy There's actually a functionality within the module allows you to do that if that's the the behavior that you want but for now I like this kind of good reading English display of Stopping at the end of one word moving on to the next one when there needs to be more room for it So okay Notice that it's using the the correct length and it's bringing about kind of like a paragraph when you loop through all of the pieces of the array in the list and display it there This kind of makes a a full string that actually contains everything, but remember it doesn't have the new lines That's why our print function comes in handy now The other function in the text wrap module will do this kind of same thing Except it will give us our new lines check it out the text wrap dot fill function wraps the single paragraph in text Which is going to be long string up here And it's going to return a single string containing the wrap paragraph fill a shorthand for new line joining all The the array that's returned in the list. It's returned by the wrap function initially Right up top here with our long string the one the wrap function we just called And it it just adds a new line at every single object We can kind of recreate this effect on our own if we wanted to but I just want to show you how it works And of course it accepts the exact same keyword arguments as the wrap function So let's check it out. I'm gonna create a little divider here. Just print this out and I'll It doesn't even have to be a separate argument. I can just I'll be part of that same line here and Now we'll print text wrap dot fill We'll pass in our long string arguments Display this and hey we get the exact same output Because you can tell that this one is actually a string and this one is looped to different lines in the array So it looks as if it were a string when we print it out now We can of course pass in that special width argument to change things up I'll create a variable for that. Just call it width and By default at 70 so let's change it to 20 And we'll see how it works pass in width to the top one when we're calling text wrap dot wrap and it'll pass in the same thing Down here we're calling text wrap dot fill so we'll have the exact same output again and This time it's wrapped to only 20 characters maximum in the line We can of course change this to like 100 and you can see it's a little bit longer more than 70 Go even more to 130 we can change this as much as we want and it'll just cut the line where it needs to Depending on the width argument, so that's the way it works Now that's what I wanted to show you in this tutorial just these two functions because I kind of go hand in hand They have similar functionality now keep in mind that both wrap and fill work by creating a text wrap or instance And then calling a single method on it that instance is not going to be reused It only happens once and then it kills itself So for applications that do this multiple times if you're going to wrap and fill many strings It's more efficient for you to just create your own text wrapper object And that's why we're gonna move on to that in the next tutorial and kind of why I want to just introduce These two functions first and then we'll move on to the next one, but thank you guys for watching I hope you enjoyed this It's been nice to be making a little bit more videos and I hope to get more out to you very very soon For now, please comment subscribe like the video do whatever you guys do and I'll see you in the next tutorial