Python Part 14d - Working with strings Office

Python Part 14d - Working with strings Office So welcome to this fourth in a four part series of tutorials on working with different data types within python in this tutorial we're going to look at strings so here's what you'll learn we'll start by looking at some basic string operations like storing strings in variables and concatenating them we look at using escape characters like backslash n to throw a carriage return and more ambitious ones we'll look at a revision of sequences and slicing how strings work as sequences and how you can take advantage of this fact.

We look at how you can edit strings for example by replacing one string of text with another and then we'll look at how you can split text into its different parts and then maybe join it back together again we'll look at how you can find one string of text within another and then how you can change the case of text so for example to make it all upper or lower case or some combination of the two we'll look at how you can pad and justify text to get me to print outs and then we'll look at how you can check.

String contents for example to see whether a string of text contains just text or just numbers or just uh some combination of the two we'll look at regular expressions for example to look at all the postcodes or the phone numbers or the zip codes in a string of text using masking and finally we'll look at the string module and we'll look at how you can translate text for example to remove all the punctuation from something at the top right of your screen about now a link should appear which you can click on to download all the many files.

To do with this tutorial if that doesn't appear you can always click on the link on the youtube tutorial page which will take you to the same web page but that's probably enough of me there's a lot to cover so i'll hand over to sven and let's get started so i want to start with a quick reminder of what strings are so we're just going to whisk through five or six things you can do with strings so firstly you could assign values to a.

Variable so i'm going to put george orwell's name in to the author name variable you can repeat strings so what i'm going to do now is create a chant of augie ogie and i must admit i knew didn't know about this i've just googled it and it turns out it came from toronto in canada so if you're watching in canada congratulations so what you can do with repeating strings is put an asterisk in and then put the number of times you.

Want to repeat it so that will give me oggy oggy ogi and if i want the rest of the chant then i will create a second variable called chart part two that will be the oi bit so that should give me ogi okay to prove this i just want to show you briefly about concatenating strings all of this should be very familiar so i could create a variable called chant which is the first bit.

Python Part 14d - Working with strings

Concatenated with the second so the plus symbol just means join two bits of text together and then i could actually print something out so if i print out my chart and try running that you'll see it gives me auggie three times and only three times another thing you can do with the string is get the length of it so i'll create a variable called length chant and set that equal to the length of it using the len function.

And print that out just to prove that's worked and that should give me 30 characters in the string of text thing i wanted to emphasize possibly the final thing in this topic is that strings are case sensitive so to prove this what i'm going to do is going back to george orwell he was at the top i'm going to find how many g's there are in his name now we'll come back to this function i'm about to use later for the moment i just want to prove case sensitivity.

So i'll create a variable called number g and set it equal to the author name and then i'm going to count how many g's there are and then print that out and i think it's only going to give me one and there it is and the reason for that is there is only one little case g in his name there it is had i added in a dot lower function there to turn his name into lower case then when i run this i'll get two g's.

Because there's two lowercase g's in the lowercase version of his name so that's some basic things you can do with strings what we'll do now is look at escape characters now i'm going to do is have a look at escape characters briefly so what i want to do is print out the wise owl name and to do that i can put the word command print and then in brackets i can say wise owl and then i want to put training but i want to put the training on a new line and so i use backslash n the reason i don't just use n.

Is if i did i would just get the message wise owl n training so the backslash tells uh python that the character which follows it is going to be treated differently in this case is going to insert a new line and if i run that program you'll see i get wisel training on two different lines i could if i liked put in backslash t instead a t means a tab character and that would give me a tab between the wise owl and the training what other escaped characters are there.

When my mom asked me how i was feeling i always used to say i was feeling peaky so let's let's say that but peaky's not really a recognized word so i'm going to put it in uh quotation marks and to do that because i'm using double quotes within double quotes i could put a backslash to say actually this doesn't denote the end of a string it's just a double quote character and then i could put peaky and then i need another backslash and another double quotes and if i run that you can see i get the.

    Message i wanted the reason you don't use escape characters for quotation marks so much in python is illustrated by the single

    Apostrophe here is because you can alternate between double and single quotes which avoids the need to use escape characters normally which is a nice feature i think so at this point you may be thinking wow how many of these escape characters are there well the most useful ones are shown in a file uh accompanying this tutorial called b table of useful characters and if you go to that you'll see the most useful.

    Characters and you've actually seen most of them already there are others but those are the only ones i think people are likely to use so that's escape characters so what we're going to do now is to summarize sequences and slicing as they apply to strings if what follows doesn't make sense to you you've never seen it before what i'd recommend doing is going back to the series of tutorials 7a 7b and 7c about the most important thing in python which.

    Is sequences this is just a quick revision so i've got a quotation here from animal farm and you can treat any quotation as or any string of text for that matter there's a string of individual letters and that means you can slice it so what i'm going to do is pick out the theme of the quote above which is probably the word liberty so to do that i'll create a variable called theme and what i'll do is take the string of text and slice it specify my star point now.

    My start point is the letter l and the letter l is the fourth character but when you're slicing of course you begin at zero so i need to put the number three in there to say i'm beginning in the fourth character then i want to pick up the seven characters in the word liberty so i'm going to put 10 in there if you remember the difference between the beginning and the end of a slice is how many characters it contains so let's see if i've got that right if i print out the value of the variable i've just created and you can see it gives me the word.

    Liberty the other thing you can do is you can loop over the elements of a string of text so you can do letters and to do this i could say if any variable let's call it letter in the theme then i can do anything you like to that letter and what it will do is treat the word liberty as a str a sequence of individual single character letters so if i now print out the letter what i'll do is get seven lines of text.

    Each one containing a separate letter in the word so that's really intended to be revision if you haven't seen this before as i say you probably do need to go back to 787b and 7c to understand sequences so what i'd like to do now is show the various ways in which you can edit strings of text in python and the example we're going to use is this we're going to take the original name of our company why is our training consultants.

    And we're going to change it to a name we thought of calling it which is armadillo trading and to do that it will have to go through a few transformations and they will tell you about the various things you can do when you're editing strings in python so i've created a variable called original text and set it to be the my starting point i seem to have missed out the word consultants from that so i'll just add it and the first thing we're going to do is we're going to strip out the blanks and this is often called trimming it.

    You can see there's blanks at the beginning and there's blanks at the end and i want to get rid of those so to do that i will create a variable called revise text and i'll set it to be the original text trimmed i keep trying to type the trim function in but it's actually called strip so now if i print that out print out the result you'll see i've got rid of my blanks it's a start so the next thing we're going to do is we're going to get rid of the y's at the.

    Beginning and the consultants at the end and to do that we're going to remove the prefix and suffix in two different commands so we'll take the revised text set it to be what it used to be but then when you type the dot in you can see a list of all the functions or methods you can apply to a string of text some of which we'll go through during this tutorial hopefully the most useful ones and we want you to choose remove prefix.

    And then in brackets you can specify which prefix you want to get rid of ours is going to be the word wise space it's

    Case sensitive so i need to put a capital w and because i'm confident that's going to work let's launch straight into the next thing which is to remove the suffix and we'll take out the word consultants and in both cases i've included the space to make sure i get rid of that so now if i run that i've still got my command to print out that at the end you'll see i get just owl training.

    I'm nearly as armadillo training already so the next thing i want to do is i want to replace the word owl with armadillo and to do this i can take the revised text set it to be what it used to be and then use the obviously named replace function and when you type in the open brackets you'll see it takes three arguments the first one is the old text what you used to be there the second one is what you're replacing it with and the third one is the number.

    Of occurrences you want to replace and normally you miss that out in which case you'll replace every single occurrence in our case it's actually irrelevant as i'll explain in a second so we're going to replace the word owl with the word armadillo and as i said the third argument is irrelevant because owl only appears once in the string owl training so it doesn't matter whether we replace one or one occurrence of it or a thousand it will only ever replace just the one word.

    So now if i try running that you can see i've got armadillo training i'm so nearly there the last thing i need to do is change that to trading and to do that another quick revision of sequencing so we're very nearly there with our armadillo trade trading there's just one more thing we need to do and help us understand how to do it i've got a helpful numeric keypad here numbering the letters in armadillo training what i want to do is replace that i n.

    With the letter d i could do that just using a replace function but i'm going to just going to show you another way using slicing just as an alternative and a bit of a revision so what i'll do is take my variable called revised text and set it to be the first 13 characters of the armadillo training then i'm going to join in the letter d and then finally i'll add on the rest of it.

    You need a fair bit of conviction that i've got the numbers right but i think i have when you run that you should get the final armadillo trading so i've really put that in there as a quick reminder that you can use slicing to manipulate strings it's obviously easy on this occasion to use a replacement method so that's how you can edit strings in python so this is another bit of revision really from previous tutorials what i want to do is look at splitting and.

    Joining back together strings of text so what we're going to do is take this quotation from animal farm and split it into the different words so to do that i can take my ver a new variable i'll call it words take my quotation and use the split function and when you use the split function you can specify what you're using to split the text up i could if i like to put a space in there which is actually what i want to do but if you miss that out all together it will assume that's what.

    You're doing anyway so that will give me the individual words to prove this has worked i could just print them all out so i could say for word in words and then i'll just print out each individual word if i then run that you'll see i get the individual words of the quotation i've got so what i'll now do is reverse this quotation for reasons which will become apparent in a second i hope to do that i can just take the.

    List as it is and reverse it and then what i want to do is i want to join it back up again and the reason i reversed it as i want to show something has changed so i'll create another variable called back quote to hold the words joined together so join words together the surprising way in which you can do this is you take the cement or the mortar you want to use to join everything together which in this case is a space and then use the join function and specify which sequence of things you.

    Want to join together which in this case is the words so now if i just print out rotation what i'll get is the sentence but in reverse order it looks a bit strange when you read it backwards because it hasn't distinguished the fact that this full stop is not a separate punctuation character it's treated as part of the word future full stop and that's why it looks a bit strange and just to complete the picture and it really is just to complete the picture there's one more thing you can do with splitting strings which is partitioning.

    Them so to show how this works i think what we'll do is just comment out all these lines of code uh to keep them in for reference and what i'm going to do now split this into different sentences by using the full stop as a separator i can do this by taking a variable called sentences and setting it equal to the quote and then what i'm going to do is partition a strange function what it does is allows you like split to split something into.

    In this case three parts the first part will be before the character or characters you set to be the splitting point the second part will be the split point itself which in this case is a full stop whoops and the third part will be everything which follows it including that last full stop so let's see if this worked if i now try printing out each sentence then you should see it gives you.

    Three bits a bit before the full stop the full stop itself and then the bit after the full stop in this case it wasn't a particularly useful thing to do because i had two full stops in but it was just really to complete the picture of what's possible with splitting and joining strings i could have used r partition that's the alternative and what the r partition function will do is split it not by the first full occurrence of this split character but by the last one so in this case i'll.

    Get if i run this three strings of text i'll get everything up to the last full stop i'll get the last full stop and then i'll get everything after it which will be blank which is what i get there so our partition in this case is even less useful than partition so in this module what we're going to do is look at how you can find one string of text within another so i've got a string of text called wisel training and what i'm going to do is look for the letter w.

    W's so to do that i will create a variable which i'll call w position it will hold the position of the first w so what i can do is type in the string of text wisel use the find word and in brackets specify the thing i'm looking for which in this case is a letter w so if i run that and print out the results then you should see that i will get not the letter w which is sorry not the number zero which is what you might be.

    Expecting because it's the zeroth position i think i'll get zero one two three four five i think it's six let's see if i'm right yeah i guess six because there is a w and ysl training at the beginning but it's in capital letters and don't forget search is a case sensitive if i were to change this to a capital w then i would get zero as a result so make sure that when you're searching you remember it's case sensitive so that's how you can search for a single letter or string of letters i'm going to um now.

    Change this so i'm just going to instead of looking wisel i'm going to look in the lowercase version of it and i'm going to look for a lowercase w now in this case when i run it i will get the letter sorry the number zero because i converted this entirely into lowercase and i'm looking for a lowercase w so it will give me the first one i find at position number zero which will be this one but what happens if i don't want to find the first one what i can then do is add some additional arguments to it.

    So i'm going to say but not the first to do this i can after the first argument put a camera in and say well actually i'm going to start at the first character and then i'll stop at the end so you can see there the extra arguments i can put the start and i can put the end in if i miss out at the end then it will assume i'm going to go all the way to the end of the string of text which is what i want to do on this occasion and if i run that now you'll see it gives me the number six.

    Because it's missed out the first character so that's how you can find one string of text within another i think the only other thing i wanted to show about finding is that you can also count the number of occurrences of string so what we're going to do is count how many so i'll create a variable called number i do this you can refer to the string of text wise l and then you can use the count method.

    And in brackets you can specify what you're looking for in this case i'm just looking for the single letter i then print that out i think i'm going to get the number three because there's one there one there and one there so run that it gives me three so that's how i can find one string of text within another so we're going to look now at changing the case of text i've got a name of a film or a movie if you're in america um.

    Called no time to die only someone's typed it in a very strange case so what i wanted to do quickly is look at the options for changing this so i can change to upper or lower case so i can print out the film name in uppercase it's a method so it takes open and close brackets after it like so or i could do the same thing and print out the fill name in lowercase i think we've seen that a few times already so if i run that program you'll see it gives me no time to die an upper and.

    Lower case but it's more likely i want to capitalize it so the options for capitalizing text are twofold i can print out the capitalized version of it spelt the american way with the zed or i could print out the title version you might like to quickly think what you'd expect to see for those two let's just print out a blank line above that so they're separated out and if i run this program you will see i.

    Get no time to die with a capital right at the very beginning that's what capitalize means the title will capitalize every single word which probably isn't what i want either i really probably want the the two with the little t and the rest capitalized but there's no function to do that i'm afraid that i know of and the final thing you can do is changing text is you can swap the case which is fun but probably ultimately fairly pointless so i could take the film and swap the case of it.

    And if i run that and again let's just add a quick uh blank line above that if i run that you'll see it gives me no time to die with every single letter in the opposite case to what it used to be so when you run run that program you'll see that the original text no time to die there has been replaced with no time to die and what python has done is change the case of every single letter i wonder if anybody has ever used that in actual anger in proper code just to complete this then there's a file uh accompanying this tutorial.

    Called g functions to change case.png and if you go to that you'll see it gives you a summary of all the text functions which might be useful as a reference point so that's how you can change case so to illustrate padding uh we're going to take a slightly different approach i've got a file which you can see accompanies this tutorial called h dash padding and it contains the finished program so if you run that you'll see that it gives you the square cube and fourth power of all the numbers from two up to.

    Ten and it's reasonably neatly aligned and what i want to show you is how you can use the are just l just and center functions or methods to achieve that so looking at the first bit of this i've got there the adjust function at the beginning and that is formatting my squares so what it's done is taken a two character string of text and it's right justifying my number within that for the next bit of it i think you can probably see where i'm going with this it then takes a square of the number and.

    It left justifies that within a five character string so if you could imagine that as five characters wide that's what's going on there for the next one it takes the cube of the number and it puts that within a five character string of text here but this time right justifying it and finally for the last bit it center justifies the fourth power so this time it's sent to justify it about across 10.

    Characters which is about there so the main reason to use l just r adjust and center is to format text when you're printing out output what we're now going to do is to look at how you can check the contents of a string of text so let's start with creating a variable which will see whether a given string of text contains just letters so i'll create my variable called if just letters and the example i'm going to use for many of these things is a dell customer services phone number i assume dell will.

    Be as happy to receive calls from us and other people as we are to receive calls incessantly from their account managers so what i'm going to do there is to say is this an alpha string does it just contain letters you can see when you let your mouse linger over that it says a string is alphabetic if all characters in the string are alphabetic and it's at least one character long so you would think that would give me a true um but actually it gives me an error message initially if i run that or at least it will if i print it out.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continue to make videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=xk_6ddqNu0g
Previous Post Next Post