< Return to Video

String methods in Python are easy 〰️

  • 0:00 - 0:01
    PROFESSOR: Hey, everybody.
  • 0:01 - 0:03
    In this topic, I'm going to
    cover a few useful string
  • 0:03 - 0:06
    methods that you may
    be interested in.
  • 0:06 - 0:08
    Then at the end of this video,
    we will work on an exercise
  • 0:08 - 0:11
    where we will validate
    some user input.
  • 0:11 - 0:14
    As we know, a string is
    just a series of characters.
  • 0:14 - 0:17
    Let's ask a user
    for their full name.
  • 0:17 - 0:20
    Name equals input.
  • 0:20 - 0:23
    Enter your full name.
  • 0:23 - 0:27
  • 0:27 - 0:29
    The first method I'll show you.
  • 0:29 - 0:31
    Well, technically
    this is a function.
  • 0:31 - 0:34
    The length function will give
    us the length of a string--
  • 0:34 - 0:36
    how many characters is it.
  • 0:36 - 0:39
    We will find the length
    of our variable name
  • 0:39 - 0:41
    after the user
    types in some input.
  • 0:41 - 0:43
    This function
    returns an integer.
  • 0:43 - 0:46
    I'll store that result
    within a variable.
  • 0:46 - 0:49
    Let's just say
    result. Then I will
  • 0:49 - 0:51
    print whatever the result is.
  • 0:51 - 0:53
  • 0:53 - 0:55
    Why don't you go ahead and
    type in your full name?
  • 0:55 - 0:58
  • 0:58 - 1:03
    The length of this string in
    my example is eight characters.
  • 1:03 - 1:05
    That does include spaces, too--
  • 1:05 - 1:08
    1, 2, 3, 4, 5, 6, 7, 8.
  • 1:08 - 1:10
    If you ever need the
    length of a string,
  • 1:10 - 1:12
    there is the length function.
  • 1:12 - 1:13
    Let's move on.
  • 1:13 - 1:17
    If we were to type our variable
    name followed by a dot,
  • 1:17 - 1:20
    we have access to a whole
    bunch of different methods.
  • 1:20 - 1:22
    We have the find method.
  • 1:22 - 1:26
    The find method will return
    the first occurrence of a given
  • 1:26 - 1:28
    character-- the position.
  • 1:28 - 1:31
    Let's find any spaces.
  • 1:31 - 1:37
    I'll store the result within
    a variable named result.
  • 1:37 - 1:41
    I will type in my full name.
  • 1:41 - 1:44
    The first occurrence of a
    space, that's what we set,
  • 1:44 - 1:46
    is at position 3.
  • 1:46 - 1:49
    When working with indexes,
    we always begin with 0.
  • 1:49 - 1:54
    This first character would have
    an index of 0, then 1, 2, 3.
  • 1:54 - 1:58
    That's why the find method
    returned 3 in place of 4.
  • 1:58 - 2:03
    Let's find the first
    occurrence of a capital B.
  • 2:03 - 2:05
    See, it's 0.
  • 2:05 - 2:12
    How about.-- oh, for
    me, that would be 2.
  • 2:12 - 2:15
    So remember, it's always
    the first occurrence.
  • 2:15 - 2:17
    If you need the last
    occurrence, there
  • 2:17 - 2:24
    is a different method, which
    is rfind, r meaning reverse.
  • 2:24 - 2:27
    We will find the last
    occurrence of an o.
  • 2:27 - 2:31
  • 2:31 - 2:33
    That has a position of 5--
  • 2:33 - 2:38
    0, 1, 2, 3, 4, 5.
  • 2:38 - 2:40
    If Python isn't able to
    locate a given character,
  • 2:40 - 2:42
    it will return negative 1.
  • 2:42 - 2:44
    Let's find any--
  • 2:44 - 2:45
    I don't know-- q's.
  • 2:45 - 2:50
  • 2:50 - 2:53
    Python could not find
    any lowercase because.
  • 2:53 - 2:55
    The rfind method will
    return negative 1
  • 2:55 - 2:57
    if there are no results.
  • 2:57 - 3:00
    We can capitalize the
    first letter in a string
  • 3:00 - 3:02
    by using the
    capitalize function.
  • 3:02 - 3:11
    Name.capitalize-- this
    method will return a string.
  • 3:11 - 3:14
    I will reassign that to name.
  • 3:14 - 3:18
    Then we will print
    our name capitalized.
  • 3:18 - 3:21
    I'll be sure to type in
    my name, all lowercase.
  • 3:21 - 3:25
    Since this is all one
    string, only the first letter
  • 3:25 - 3:29
    is capitalized, even though I'm
    including a first and last name.
  • 3:29 - 3:33
    The upper method will take all
    of the characters in a string,
  • 3:33 - 3:37
    then make them all uppercase
    Follow your variable that
  • 3:37 - 3:40
    contains a string
    followed by .upper.
  • 3:40 - 3:44
    Then I will reassign the
    result to my name variable
  • 3:44 - 3:45
    to overwrite it.
  • 3:45 - 3:48
    Enter your full name.
  • 3:48 - 3:51
    All of the letters
    are now uppercase.
  • 3:51 - 3:55
    There is also lower to make all
    of the characters lowercase.
  • 3:55 - 3:59
    Name equals name dot lower.
  • 3:59 - 4:03
  • 4:03 - 4:06
    Yep, all the characters
    are lowercase now.
  • 4:06 - 4:11
    The isdigit method will
    return either true or false
  • 4:11 - 4:14
    if a string contains
    only digits.
  • 4:14 - 4:15
    The result is a Boolean--
  • 4:15 - 4:17
    true or false.
  • 4:17 - 4:19
    I'll store that within
    a variable named result,
  • 4:19 - 4:22
    then print result.
  • 4:22 - 4:28
    So if I were to type in my full
    name, isdigit returns false.
  • 4:28 - 4:31
    There are not only digits
    within that string.
  • 4:31 - 4:33
    If my string was
    some combination
  • 4:33 - 4:36
    of alphabetical
    characters and numbers,
  • 4:36 - 4:38
    this method will
    still return false.
  • 4:38 - 4:44
    It only returns true if my
    string only contains digits.
  • 4:44 - 4:45
    I'll just type in 1, 2, 3.
  • 4:45 - 4:47
    See, that's true.
  • 4:47 - 4:50
    That is the isdigit method.
  • 4:50 - 4:54
    Otherwise, we have isalpha--
  • 4:54 - 4:54
    name.isalpha.
  • 4:54 - 4:57
  • 4:57 - 5:00
    The isalpha method
    will return a Boolean--
  • 5:00 - 5:04
    true or false-- depending
    if a string contains
  • 5:04 - 5:06
    only alphabetical characters.
  • 5:06 - 5:08
    I'll type in my full name.
  • 5:08 - 5:10
    So the reason that
    this came up false
  • 5:10 - 5:14
    is because my full name
    contains a space, which is not
  • 5:14 - 5:17
    an alphabetical character.
  • 5:17 - 5:21
    If I typed in my full
    name, excluding any spaces,
  • 5:21 - 5:22
    this would now be true.
  • 5:22 - 5:26
    isalpha would also return
    false if my name contained
  • 5:26 - 5:27
    any sort of digits--
  • 5:27 - 5:28
    0, 1, 2, 3.
  • 5:28 - 5:30
    And that is also false.
  • 5:30 - 5:34
    That is the isalpha method.
  • 5:34 - 5:38
    Now let's ask for
    a phone number.
  • 5:38 - 5:44
    Phone number equals input.
  • 5:44 - 5:48
    Enter your phone number.
  • 5:48 - 5:51
    With the phone number, they
    typically contain dashes.
  • 5:51 - 5:53
    Let's count how many dashes are
    going to be in somebody's phone
  • 5:53 - 5:56
    number.
  • 5:56 - 6:02
    Phone number dot count method.
  • 6:02 - 6:04
    Let's count the
    amount of dashes.
  • 6:04 - 6:08
    So place a character
    within the count method.
  • 6:08 - 6:10
    This method will
    return an integer.
  • 6:10 - 6:12
    Let's store that
    within a variable.
  • 6:12 - 6:17
    Result equals phone
    number dot count method.
  • 6:17 - 6:19
    So type in some phone number--
  • 6:19 - 6:26
    1-234-567-8901.
  • 6:26 - 6:28
    We have three dashes
    within the string--
  • 6:28 - 6:31
    1, 2, 3.
  • 6:31 - 6:32
    That is the count method.
  • 6:32 - 6:37
    We can count how many
    characters are within a string.
  • 6:37 - 6:39
    We also have the replace method.
  • 6:39 - 6:41
    Honestly, the replace
    method is probably
  • 6:41 - 6:44
    one of the most useful
    methods of strings.
  • 6:44 - 6:48
    We can replace any occurrence
    with one character with another.
  • 6:48 - 6:49
    Replace.
  • 6:49 - 6:55
    Let's replace any dashes
    with maybe a space.
  • 6:55 - 6:57
    This method will
    return a new string.
  • 6:57 - 7:01
    I'm going to reassign this
    to our phone number variable,
  • 7:01 - 7:03
    then print the phone number.
  • 7:03 - 7:05
    Enter your phone number.--
  • 7:05 - 7:10
    1-234-567-8901.
  • 7:10 - 7:12
    So here's my new phone number.
  • 7:12 - 7:15
    But we've replaced all of
    the dashes with spaces.
  • 7:15 - 7:18
    Even better yet, we could
    eliminate all the dashes
  • 7:18 - 7:22
    completely by replacing the
    dashes or another character
  • 7:22 - 7:25
    with an empty string.
  • 7:25 - 7:31
    1-234-567-8901.
  • 7:31 - 7:34
    Here's our new phone
    number without any dashes.
  • 7:34 - 7:37
    We've replaced all dashes
    with an empty string.
  • 7:37 - 7:38
    No characters.
  • 7:38 - 7:40
    If you would like a
    comprehensive list of all
  • 7:40 - 7:43
    of the string methods
    available to you,
  • 7:43 - 7:44
    you can use the help function.
  • 7:44 - 7:48
    Type in the data type
    str meaning string.
  • 7:48 - 7:50
    Then I will print
    whatever the result is.
  • 7:50 - 7:55
  • 7:55 - 7:57
    Here's a bunch of
    methods you might
  • 7:57 - 7:59
    be interested in the future--
  • 7:59 - 8:04
    capitalize, casefold, center,
    count, encode, endswith--
  • 8:04 - 8:06
    just to name a few.
  • 8:06 - 8:09
    All right, everybody,
    here's an exercise for you.
  • 8:09 - 8:11
    We will validate
    some user input.
  • 8:11 - 8:15
    We would like a user to
    enter in a valid username.
  • 8:15 - 8:17
    However, there's a couple rules.
  • 8:17 - 8:21
    The username can be no more
    than 12 characters long.
  • 8:21 - 8:24
    The username must not
    contain any spaces,
  • 8:24 - 8:29
    and the username must
    not contain any digits.
  • 8:29 - 8:35
    Let's assign a variable
    named username equals input.
  • 8:35 - 8:38
    Enter a username.
  • 8:38 - 8:40
  • 8:40 - 8:44
    First, let's check to
    see if our user input is
  • 8:44 - 8:45
    more than 12 characters long.
  • 8:45 - 8:48
    We can do that using
    the length function.
  • 8:48 - 8:52
    We will find the
    length of our username.
  • 8:52 - 8:54
    The length function
    returns an integer.
  • 8:54 - 8:58
    Let's check to see if the
    length of our username
  • 8:58 - 9:01
    is greater than 12 characters.
  • 9:01 - 9:05
    If it is, we'll print a message.
  • 9:05 - 9:14
    Your username can't be
    more than 12 characters,
  • 9:14 - 9:20
    else we will print
    using an f string--
  • 9:20 - 9:25
    welcome, whatever our
    username variable is.
  • 9:25 - 9:26
    Let's try it.
  • 9:26 - 9:31
    I'll type in my first
    name, last name, then
  • 9:31 - 9:34
    add a whole bunch of
    characters afterwards.
  • 9:34 - 9:38
    Your username can't be
    more than 12 characters.
  • 9:38 - 9:43
    Let's type in something
    that's under 12 characters.
  • 9:43 - 9:45
    Yep, and that appears to work.
  • 9:45 - 9:49
    OK, so we have
    accomplished task number 1.
  • 9:49 - 9:52
    Our username can't be
    more than 12 characters.
  • 9:52 - 9:55
    Next, our username must
    not contain any spaces.
  • 9:55 - 9:58
  • 9:58 - 10:01
    We can use the find
    method of a string.
  • 10:01 - 10:05
    Username.find-- we
    will find any spaces.
  • 10:05 - 10:07
    That's a character.
  • 10:07 - 10:12
    If no spaces are found, this
    method will return negative 1.
  • 10:12 - 10:17
    Using an elseif
    statement, I'll add
  • 10:17 - 10:27
    not if the find method of
    username equals negative 1.
  • 10:27 - 10:33
    If the result is not negative
    1, meaning we found a space,
  • 10:33 - 10:42
    we will print your username
    can't contain spaces.
  • 10:42 - 10:45
    I'll type in my
    first and last name.
  • 10:45 - 10:47
    You might need to think
    of something that's
  • 10:47 - 10:50
    underneath 12 characters.
  • 10:50 - 10:52
    Your username can't
    contain spaces.
  • 10:52 - 10:55
    So we have accomplished
    rule number 2.
  • 10:55 - 10:59
    Three, username must
    not contain digits.
  • 10:59 - 11:03
    We can use the isalpha
    method of strings.
  • 11:03 - 11:06
    The isalpha method
    returns a Boolean
  • 11:06 - 11:10
    if a string only contains
    alphabetical characters.
  • 11:10 - 11:12
    So let's copy that.
  • 11:12 - 11:14
    I'll add another
    elseif statement,
  • 11:14 - 11:17
    not username is alpha.
  • 11:17 - 11:21
  • 11:21 - 11:31
    Then we will print your
    username can't contain numbers.
  • 11:31 - 11:34
    I guess technically, isalpha
    would check for spaces, too,
  • 11:34 - 11:37
    but I'd rather have that be
    handled within a different if
  • 11:37 - 11:39
    statement.
  • 11:39 - 11:43
    All right, I'll
    type in a username.
  • 11:43 - 11:45
    I'll include some digits.
  • 11:45 - 11:49
    Your username can't
    contain numbers.
  • 11:49 - 11:49
    All right.
  • 11:49 - 11:51
    I think we've accomplished this.
  • 11:51 - 11:55
    Let me make up a username
    following these three rules.
  • 11:55 - 11:57
    Yep.
  • 11:57 - 11:58
    It seems to check out.
  • 11:58 - 11:59
    All right, everybody.
  • 11:59 - 12:01
    And that is a few
    useful string methods
  • 12:01 - 12:04
    that you may be interested in.
  • 12:04 - 12:05
Title:
String methods in Python are easy 〰️
Description:

more » « less
Video Language:
English
Duration:
12:06

English subtitles

Revisions