ALL 47 STRING METHODS IN PYTHON EXPLAINED

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's lesson we're going to be going over all of the string Methods that come with the string in Python so I mean there's a lot of common ones such as split or upper that you are probably familiar with but there are also a lot that aren't really that commonly used and I want to just cover every single one of these so there are going to be 47 in total excluding the dunder methods so in case you're curious about everything you can do with a string follow along with this video because we're going to be covering 47 of these string Methods for number one we have capitalize which means if we have some text and that's in lowercase or uppercase we can capitalize that and when we run this we're going to get hello with a capital H and if this is in uppercase it's still going to capitalize that string next we have case fold which returns a version of the string for caseless comparisons so pretend you have Mario here that's spelled a bit weird if we run this it's going to make sure that we can compare this to another Mario without having to worry about whether this is uppercase or lowercase so if we duplicate this line and say text 2 and change this to Mario like that as you probably know this is not going to be equal to this because of the case sensitivity but if we print both of them text 1 and text two they're both going to be processed into a version that can be compared without having to worry about the case next we have the center method so suppose we have some text and we want to call Dot Center on it provide some sort of number and that number is going to specify how many characters you want this string to occupy and where you want the text to be centered and optionally you can include a character or a string that's going to be placed around the string that you want to Center number four is Count suppose you have a string with some sort of substring that you want to count here if we type in ABC underscore ABC underscore ABC and so on and then we use the method dot count with only a b we're going to get a return of 4 because it's counting all the occurrences of a b in this string for number five we have encode which allows us to encode a string of our choice and if we run this we're going to get the byte version of Elon Musk because we were able to encode that into utf-8 and you can also provide an optional parameter which says errors and if you set this to strict it means that if it cannot encode it it's going to throw an error which is good in case you want to make sure that this does become encoded for number six we have the ends with method which checks that a string ends with a certain letter such as e if we check now that Apple ends with e we're going to get a return of true and this doesn't have to be a single character it can actually be a tuple of characters such as e and we can also say a so now it's going to check that it ends with either e or a and if we run that we're still going to get true and if we type in apla it will still be true but if we type in something such as Zed it's going to return false because neither e nor a was at the end of our string number seven expand tabs right here we have a string with some tabs as you can see by the backslash T using dot expand tabs allows us to expand those tabs using spaces so here I defined a value of 20 which means when we run this you'll see that we'll get some very long tabs inside our string number eight find suppose we have a string that says something silly such as remember to comment and subscribe and suppose we want to find the substring of subscribe in this string well we can do that using the text Dot find method and it's going to return to us the integer of the first and lowest occurrence of subscribe so if we have multiple subscribes it's going to return the first one that it finds now if we run this we're going to find that subscribe was located at the index of 24 so I went ahead and printed everything after that position and that returned to me subscribe now if we don't have what we are looking for such as as does it's going to return to us the index of negative one number nine format here are two different ways we can use this method with strings the first way is defining some keywords inside the string followed by some curly brackets and then typing in text.format followed by the keyword arguments such as subject and action which I've included in this string now subject is going to be replaced by cat and action is going to be replaced by meow and the same thing works without having to specify the keywords now when we run both of these it's going to say cat is doing meow next we have format map which Maps a dictionary to the formatted values that we have in our string so for example if we have a dictionary with X and Y and then we have a string using those values we can just call text Dot format map coordinates and it's going to place these values in the given values down here so if we run this we'll get that the coordinates are 10 and -5 number 11 Index right here we have a string that says astronauts recently discovered a banana on the moon question mark and we want to find the position of banana now recently I showed you that we can use find to find the position of a certain substring and we can achieve the same thing with index except index does have one difference and that is that if we do not find what we're looking for it's going to throw an error instead of returning to us minus one number 12 is al-num which stands for is alphanumeric and that checks that the string contains letters or numbers but if it contains something such as an exclamation mark or some other symbol it's not going to be considered alphanumeric anymore so right now if we run this with hello kitty123 it is alphanumeric because it only contains letters and numbers but if we add an exclamation mark for example it's going to evaluate two false because an exclamation mark is neither a number nor a letter number 13 is Alpha and that checks that the text is alphabetic only so if we run this it's going to return false because Hello Kitty contains numbers but if we remove the numbers it's going to evaluate to True number 14 is Ashi and that returns to us true if the current string contains only ashy characters so if we decide to add these symbols such as a copyright Mark to our text it's going to return false to us because that is not part of the Ashi table but if we exclude that it's going to return true to us now for 15 16 and 17 I decided to bring in my notes for this because these are quite confusing if you don't have them side by side so what we're covering now is is decimal is digit and is numeric so right now if you want to make sure that something is actually a number you can use is decimal and that's going to evaluate one two three to true and if you run that the first one is going to evaluate to true but if we decide to evaluate something that is numeric but not a decimal such as these crazy symbols here it's going to evaluate to false and that is because it is only checking that this is a decimal now is digit checks for something very similar it checks that the current string is a digit and one two three four five six even in these crazy bubbles is considered a digit so it's going to evaluate to true now numeric on the other hand is going to just evaluate the true if the number happens to be numeric so all these Japanese numbers do evaluate the numbers so it's considered numeric but this does not work if you put it inside here because it is not a digit now the reason I brought all three of these together is just to show you that they actually work in quite similar ways and that is that is numeric works with all three is digit works with these two and is decimal only works with numbers so what I'm trying to say is that if you wanted to include some numbers inside is numeric such as one two three it will still evaluate to true if you wanted to include some crazy symbols like these ones that are digits it will still evaluate to true but this does not work the other way around so if we take these Japanese numbers and place them inside the is digit it's going to evaluate two false because it is not a digit even though it is numeric but for the most part of the time you probably just want to check that it is a decimal so I would recommend using that but in case you want to check whether something is numeric or is a digit you also have that option number 18 is identifier here we have some text that says test sample and what this does is check that it is a valid identifier name in Python in case you want to use it to name a variable for example if we run this we're going to get false because you cannot use dashes in names in Python if we change it to an underscore it will work but if we change this to a one test underscore sample it will still return false because numbers cannot proceed variable names in Python number 19 is lower here we have some text with ABC with a uppercased and what this does is check that the entire string is lowercased if we run this we're going to get false as a return because ABC has a capital A but if we return it with a lowercase it's going to evaluate to True number 20 is printable all this does is check that the current line of text is actually printable and it considers text to be printable if it doesn't contain any Escape characters such as new line if we run this right now we're going to get false because it contains this escape sequence but if we return it without that escape sequence we're going to get true as a return number 21 is space all this does is check that a string only contains space so if we create this string with a lot of empty space and run the program we're going to get true as a return because it is only space number 22 is title is title checks that the given string is a title so that video is going to elaborate to true because it is a title it follows the title syntax if we type in something such as hello video it's going to evaluate the false because hello is not uppercased so all it's really doing is checking that that the first letter of each word is uppercased number 23 is upper here we have a string with bananas in complete uppercase and all this does is check that the entire string is uppercased here we're going to get a return of true because bananas is uppercased but if we add a lowercase n it's going to evaluate the false because it is not entirely uppercased number 24 join right now we have a text of type string and we want to combine some words for example so what join does is combine an array of items together using the given string so this is going to be placed in between each one of the elements that we are joining and to call it we just have to say text Dot join and we're going to join an array of our choice so if we run this we'll get text 1 dash text 2-text3 and you will probably see it without this because it is an extra line of code that you don't need and used instead like this with a dash and with that we will still get the same result but without having to use this extra variable next we have L just which aligns the text as far as possible to the left with the amount of space that we've provided so here we have text.eljust and I decided to give it 20 characters of space and to fill the remaining with the underscore so if we run this we're going to get text and underscore field for the remaining spots inside this string number 26 lower right now we have a string that has been uppercased and all we want to do is convert it to lowercase and we can use that using text Dot lower so uppercase in uppercase is going to become an uppercase in lowercase number 27 L strip here we have some text and pretend you want to remove some from some text we can do that using dot L strip followed by the keyword or the text that you want to remove from the left side of the string so if we remove that we will just have text remaining and if we do sum with some space it's also going to remove the space in front of text number 28 and 29 make trans and translate these two tend to go hand in hand when you are working with them because what make trans does is create a translation table so right now what we're saying is that we want to convert this text to a translation table and to do that we want to make sure that every uppercase b gets translated to this Emoji over here and then we want to use that translation table for the text that we want to print because right now if we print this text as it is even if we've created a table it's not going to do anything but if we translate it with the table that we've created here it's going to replace each uppercase B with this emoji and if we run that we'll see that 66 or that character was replaced with this emoji character and that's why we're going to get this funky string over here and the cool thing about that is that you can use any new string you want it can even be that is bacon baby and when you run this we're going to get each one of the uppercase B's replaced with this symbol over here if you want to have more than just one character converted you can enter your own dictionary key and it's going to create a translation table number 30 partition suppose you have a string such as a plus b equals c and you want to partition these in other words split them at a certain point you can use the dot partition method to make sure that you split this into three equal strings at the index of equal and what that really means is that we're using equals as a separator to tell the program that we want to split this string into three parts between the equal sign so we're going to get back a plus b equals and C number 31 remove prefix here we have a string that says and what we want to do is remove the beginning part which says was to do that we just type in dot remove prefix and all we're going to get back is app now for number 32 it's exactly the opposite we are removing the suffix so here we have Mr everyone and we want to remove one from this so here we'll just type in text Dot remove suffix with one and when we run that we'll get Mr every now number 33 is one of the most important ones because it tells you to remember to comment I'm just joking about that of course but here we have a string that says remember to comment and all we're going to do is replace it with subscribe so here you can choose a substring that you want to replace and replace it with the current string that you've provided so if we run that instead of saying remember to comment it's going to say remember to subscribe and optionally in case you have many occurrences of the same word you can provide a limit to how many times you want to replace that text so instead of replacing every occurrence of comment with subscribe we're only going to replace the first occurrence so now it's going to say remember to subscribe and to comment number 34 are find here we have some text that says a some text Dot a and what we want to do is find the first occurrence of a starting from the right so here if we type in text Dot our find and we print the position we're going to get the latter and not the first if we use the original we're going to get the first one and just as before if you type in something that doesn't exist you're going to get -1 when you use find when you use index you're going to get an error but what better way to explain that than using the exact same example except with our index which does the same thing as index except it starts from the end and searches in this direction so if we look for B it's going to return to us 14 because it's starting from the right side and again if you type in something that doesn't exist it's going to give us a value error number 36 are justify here we have some text and we're just going to say text dots are just and we want to make sure that this string occupies 20 spaces and we're going to fill the remaining with the underscore and as you can see in the output that's what R just does now at this point you know what partition does it splits the string into three depending on the separator that you've defined but we also have something called R petition which tells us which tells the program to start from the right side so instead of getting text and having this as a divider we're going to be using the second one instead because it use because it starts from the right side and if we run that you'll see text equals text 2 equals but if we remove the r from our partition it's going to do it the opposite way 38 and 39 are splits and split but first we're going to cover our split so here we have some text that says this is some special stuff and what our split does is starting from the right it splits the string into an array given the separator that we've provided so as you can see here we have some space that we decided to use as a separator so it's going to split each and every one of these words into its own element and if we run that we'll get this is some special stuff because we used the space as a separator and the same thing goes for this website over here we decided to use a DOT as a separator so it's separated w w website and com now text.split is the exact same thing except it evaluates this from the left side so it starts splitting in this direction which means that if we run this program right now and provide a Max split of two it's going to split this two times at most so this is some special stuff as you can see it separated these two but it did not separate the rest because we said we're only going to separate the first two elements and the reason I used Max split here is because I wanted to show you that if we use R split with Max split it's going to do it in the opposite direction because it starts from the right side first next we have R strip which strips the text from the right side so here we have text.r strip and we want to strip Mario from the right side and since we have two Marios here it's going to remove this Mario right there and now we'll have an output of his name is Mario 41 we have split lines so suppose you have some text and the text has some separators such as slash n which starts a new line text Dot split lines is going to take each one of those lines and put it as an element into an array so if we run this we'll get remember to comment as two different elements in an array now you can specify this parameter keep ends to be true or false depending on whether you want to keep these escaped characters without it you're just going to get D strings without the backslash n included in both of the elements number 42 starts with suppose you have a string that starts with L you can check that by using the dot starts with method and it's going to check that Luigi starts with L which is true but Luigi does not start with f and that can be proven by using this method number 43 strip suppose you want to remove Luigi from this string you can just type in text Dot strip and here we're just removing Luigi by inserting Luigi so by running this we will effectively remove Luigi and this one works for both sides so you can either remove pasta or Luigi depending on your preference as you can see if we type in past that dot it will also remove pasta number 44 swap case here we have a string that says Luigi has pasta and let's just swap case that the easiest way to explain this is to run it now we have Luigi has pasta in a swap case now one that's really interesting in Python that can help you a lot in case you don't know how to write a title is the dot title method here I wrote something that's called This is a title and what this is a title do is make sure that each letter is uppercased in your title so I would say simple way to make a title and I think this is one of my favorite string Methods because of it number 46 upper here we have some text that says hello there and we just want to uppercase that and if we run this we'll get hello there in screaming characters and finally we have number 47 which is also the final method that we will be covering in this video and that is zero fill so right now we have some text and if we just run this as it is you'll see that text is going to be filled with some zeros just in case you have to add some zeros to your text but anyways those were 47 string Methods that you can use in Python do let me know in the comment section down below if you learned something new or if there was something that I missed regarding these methods this video took me 50 minutes to record and it's actually even growing longer because of this explanation but if you did enjoy the video do consider leaving a like and a comment I love to read those but otherwise with that being said I also want to wish all of you a Happy New Year I know this is quite late maybe it's day five day six of the year but this is the first video I'm recording this year so I thought I might as well mention it otherwise with that being said as always thanks for watching and I'll see you in the next video oh
Info
Channel: Indently
Views: 120,655
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: bnSYeYFRCaA
Channel Id: undefined
Length: 23min 33sec (1413 seconds)
Published: Sun Jan 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.