String methods in Python are easy 🧡

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody in this topic I'm going to cover a few useful string Methods that you may be interested in then at the end of this video we will work on an exercise where we will validate some user input as we know a string is just a series of characters let's ask a user for their full name name equals input enter your full name the first method I'll show you well technically this is a function the length function will give us the length of a string how many characters is it we will find the length of our variable name after the user types in some input this function returns an integer I'll store that result within a variable uh let's just say result then I will print whatever the result is why don't you go ahead and type in your full name the length of this string in my example is eight characters that does include spaces too one two three four five six seven eight if you ever need the length of a string there is the length function let's move on if we were to type our variable name followed by a DOT we have access to a whole bunch of different methods we have the find method the find method will return the first occurrence of a given character the position let's find any spaces I'll store the results within a variable named result I will type in my full name the first occurrence of a space that's what we set is at position three when working with indexes we always begin with zero this first character would have an index of zero then one two three that's why defined method returned three in place of four let's find the first occurrence of a capital B see it's zero how about oh for me that would be two so remember it's always the first occurrence if you need the last occurrence there is a different method which is R find R meaning reverse we will find the last occurrence of an o that has a position of five zero one two three four five if python isn't able to locate a given character it will return negative one let's find any I don't know uh cues python could not find any lowercase cues the R5 method will return negative one if there are no results we can capitalize the first letter in a string by using the capitalize function name dot capitalize this method will return a string I will reassign that to name then we will print our name capitalized I'll be sure to type in my name all lowercase since this is all one string only the first letter is capitalized even though I'm including a first and last name the upper method will take all of the characters in a string then make them all uppercase follow your variable that contains a string followed by Dot Upper then I will reassign the result to my name variable to overwrite it add to your full name all of the letters are now uppercase there is also lower to make all of the characters lowercase name equals name dot lower yep all the characters are lowercase now the is digit method will return either true or false if a string contains only digits the result is a Boolean true or false I'll store that within a variable named result then print result so if I were to type in my full name is digit returns false there are not only digits within that string if my string was some combination of alphabetical characters and numbers this method will still return false it only returns true if my string only contains digits I'll just type in one two three see that's true that is the is digit method otherwise we have is Alpha name dot is Alpha the is Alpha method will return a Boolean true or false depending if a string contains only alphabetical characters I'll type in my full name so the reason that this came up false is because my full name contains a space which is not an alphabetical character if I typed in my full name excluding any spaces this would now be true is Alpha would also return false if my name contained any sort of digits bro one two three and that is also false that is the is Alpha method now let's ask for a phone number phone number equals input enter your phone number with the phone number they typically contain dashes let's count how many dashes are going to be in somebody's phone number phone number Dot count method let's count the amount of dashes so place a character within the account method this method will return an integer let's store that within a variable result equals phone number dot count method so type in some phone number 1-234-567-8901 we have three dashes within the string one two three that is the count method we can count how many characters are within a string we also have the replace method honestly the replace method is probably one of the most useful methods of strings we can replace any occurrence with one character with another replace let's replace any dashes with maybe a space this method will return a new string I'm going to reassign this to our phone number variable then print the phone number add to your phone number 1-234-567-8901 so here's my new phone number but we've replaced all of the dashes with spaces even better yet we could eliminate all the dashes completely by replacing the dashes or another character with an empty string 1-234-567-8901 here's our new phone number without any dashes we've replaced all dashes with an empty string no characters if you would like a comprehensive list of all of the string Methods available to you you can use the help function type in the data type Str meaning string then I will print whatever the result is here's a bunch of methods you might be interested in in the future capitalize case fold Center count and code ends with just to name a few all right everybody here's an exercise for you we will validate some user input we would like a user to enter in a valid username however there's a couple rules the username can be no more than 12 characters long the username must not contain any spaces and the username must not contain any digits let's assign a variable named username equals input enter a user name first let's check to see if our user input is more than 12 characters long we can do that using the length function we will find the length of our username the length function returns an integer let's check to see if the length of our username is greater than 12 characters if it is we'll print a message your username can't be more than 12 characters else we will print using an upstring welcome whatever our username variable is let's try it I'll type in my first name last name then add a whole bunch of characters afterwards your username can't be more than 12 characters let's type in something that's under 12 characters yep and that appears to work okay so we have accomplished task number one our username can't be more than 12 characters next our username must not contain any spaces we can use the find method of a string username dot find we will find any spaces that's a character if no spaces are found this method will return negative one using an else if statement I'll add not if the find method of username equals negative one if the result is not negative one meaning we found a space we will print your username can't contain spaces I'll type in my first and last name you might need to think of something that's underneath 12 characters your username can't contain spaces so we have accomplished rule number two three username must not contain digits we can use the is Alpha method of strings the is Alpha method returns a Boolean if a string only contains alphabetical characters so let's copy that I'll add another else if statement and not username is Alpha then we will print your username can't contain numbers I guess technically is Alpha would check for spaces too but I'd rather have that be handled within a different if statement all right I'll type in a username I'll include some digits your username can't contain numbers all right I think we've accomplished this let me make up a username following these three rules yep it seems to check out all right everybody and that is a few useful string Methods that you may be interested in
Info
Channel: Bro Code
Views: 87,167
Rating: undefined out of 5
Keywords: python string methods, string methods Python, python, string, methods
Id: tb6EYiHtcXU
Channel Id: undefined
Length: 12min 5sec (725 seconds)
Published: Mon Oct 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.