< Return to Video

31 ESSENTIAL Python String Methods

  • 0:00 - 0:02
    PROFESSOR: Strings are an
    essential data type in Python
  • 0:02 - 0:05
    that are used in nearly
    every application.
  • 0:05 - 0:07
    And in this tutorial, we
    learn about the most essential
  • 0:07 - 0:09
    built-in string methods.
  • 0:09 - 0:11
    So after watching
    this video, you
  • 0:11 - 0:13
    will be equipped with
    all the knowledge
  • 0:13 - 0:16
    you need to work with strings
    easily and modify them
  • 0:16 - 0:17
    without any problems.
  • 0:17 - 0:18
    So let's get started.
  • 0:18 - 0:20
    Number 1 is slicing.
  • 0:20 - 0:22
    So with slicing, we
    can use this syntax.
  • 0:22 - 0:25
    And this means we
    access a substring.
  • 0:25 - 0:28
    So now, if we run this, we
    still get the whole string.
  • 0:28 - 0:31
    But now we can put in an
    optional start and also
  • 0:31 - 0:33
    an optional stop index.
  • 0:33 - 0:36
    So now if we run this, this
    means we get only the substring
  • 0:36 - 0:41
    from position 3 to position
    7, and 8 is excluded.
  • 0:41 - 0:43
    Number 2 is the strip method.
  • 0:43 - 0:46
    So by default, this will
    remove all leading and trailing
  • 0:46 - 0:48
    whitespace characters.
  • 0:48 - 0:51
    So, for example, spaces,
    tabs, and newline characters.
  • 0:51 - 0:54
    So if we run this,
    we simply get "hello"
  • 0:54 - 0:56
    without the spaces in the
    beginning and the end.
  • 0:56 - 0:59
    By default, strip removes
    all leading and trailing
  • 0:59 - 1:01
    whitespace characters.
  • 1:01 - 1:03
    But we can also put
    in a character here.
  • 1:03 - 1:07
    And now it removes all leading
    and trailing given characters.
  • 1:07 - 1:09
    So now, if we run
    it, then it removes
  • 1:09 - 1:11
    the leading and trailing hashes.
  • 1:11 - 1:14
    But be careful here
    because sometimes we forget
  • 1:14 - 1:17
    that this removes only leading
    and trailing found matches.
  • 1:17 - 1:21
    So if we run this, then it still
    has the whitespace characters
  • 1:21 - 1:22
    in the beginning.
  • 1:22 - 1:25
    For example, this
    new line and the tab.
  • 1:25 - 1:30
    And this is because the very
    first leading sign is not
  • 1:30 - 1:31
    a found match for this.
  • 1:31 - 1:35
    So if we put it
    like this, then we
  • 1:35 - 1:38
    have a leading whitespace,
    a leading new line
  • 1:38 - 1:39
    character again.
  • 1:39 - 1:41
    So now, if we run this,
    then this got removed.
  • 1:41 - 1:45
    We can also pass in multiple
    characters to the strip method,
  • 1:45 - 1:48
    but now it will not interpret
    this as one single string.
  • 1:48 - 1:51
    But instead, it will look for
    all the occurrences of all
  • 1:51 - 1:53
    the single given characters.
  • 1:53 - 1:58
    For example, here it will remove
    all the c's, m's, o's, w's,
  • 1:58 - 1:59
    and all dots.
  • 1:59 - 2:01
    So for this string,
    it removes all
  • 2:01 - 2:03
    the W's and the dot
    in the beginning
  • 2:03 - 2:06
    and then also this dot
    and the c, o, and the m.
  • 2:06 - 2:10
    So if we run this, we only
    get example as a string back.
  • 2:10 - 2:14
    Number 3 and 4 are
    lstrip and rstrip.
  • 2:14 - 2:17
    So if we only want to remove
    white spaces on one side,
  • 2:17 - 2:20
    then we could, for
    example, use lstrip.
  • 2:20 - 2:23
    This means we remove only
    the leading white space
  • 2:23 - 2:26
    so we can see we still have
    the trailing white space
  • 2:26 - 2:27
    in our output.
  • 2:27 - 2:31
    Or if we use rstrip, then
    we remove only the trailing
  • 2:31 - 2:33
    whitespace characters.
  • 2:33 - 2:36
    So now we see we still have
    the spaces in the beginning.
  • 2:36 - 2:39
    Number 5 and 6 are remove
    prefix and remove suffix.
  • 2:39 - 2:43
    So like I said, if we put in
    multiple characters to a strip
  • 2:43 - 2:47
    function, then it removes all
    the single given characters.
  • 2:47 - 2:52
    So in this example, it removes
    also this t, this h, and this r.
  • 2:52 - 2:55
    So if we run this, then we
    get only this part back.
  • 2:55 - 2:58
    And if we are really
    looking to only remove
  • 2:58 - 3:02
    this particular substring
    in the beginning,
  • 3:02 - 3:04
    then we can use remove prefix.
  • 3:04 - 3:08
    So if we run this, then we
    only get this part back.
  • 3:08 - 3:11
    And similar, we can
    use remove suffix.
  • 3:11 - 3:14
    So this will remove this part
    if it finds it in the end.
  • 3:14 - 3:17
    So if we run this, then
    we only get hello back.
  • 3:17 - 3:18
    Number 7 is the replace method.
  • 3:18 - 3:22
    So this will replace all the
    occurrences of this given
  • 3:22 - 3:23
    string with this given string.
  • 3:23 - 3:27
    So in this case, it replaces
    all the spaces with a dash.
  • 3:27 - 3:29
    So our string will
    look like this.
  • 3:29 - 3:32
    And this is also sometimes
    used to remove elements.
  • 3:32 - 3:35
    So if we put in an
    empty string here,
  • 3:35 - 3:37
    then this will remove
    all the spaces.
  • 3:37 - 3:39
    Number 8 is re.sub.
  • 3:39 - 3:41
    So this is a regular
    expression method.
  • 3:41 - 3:45
    And this is useful when we
    want to replace a more complex
  • 3:45 - 3:46
    pattern with a substring.
  • 3:46 - 3:50
    For example, in this string, we
    want to replace all the spaces
  • 3:50 - 3:51
    with a dash.
  • 3:51 - 3:55
    But in this part, we only
    want to use one dash.
  • 3:55 - 3:58
    So if we use the replace
    method like before,
  • 3:58 - 4:02
    then it will replace all
    the spaces with a dash.
  • 4:02 - 4:05
    So this means we get
    four dashes here.
  • 4:05 - 4:08
    So in order to only
    get one, we need
  • 4:08 - 4:10
    to use a regular expression.
  • 4:10 - 4:14
    And with this, we can use
    re.sub and then use the pattern.
  • 4:14 - 4:18
    So this will remove multiple
    whitespace characters
  • 4:18 - 4:19
    with a dash.
  • 4:19 - 4:23
    And if we assign this back
    to a string and run this,
  • 4:23 - 4:25
    then our string looks like this.
  • 4:25 - 4:28
    Number 9 is the split function.
  • 4:28 - 4:31
    This will return a list of
    the words in the string.
  • 4:31 - 4:33
    So in this case,
    if we run this, we
  • 4:33 - 4:36
    get a list of all
    the single words.
  • 4:36 - 4:40
    And by default, it uses
    a space as a delimiter.
  • 4:40 - 4:43
    So for example, we can
    also specify a comma here.
  • 4:43 - 4:47
    And then it would be looking
    for a comma as a separator.
  • 4:47 - 4:52
    And we can also use
    a max split argument.
  • 4:52 - 4:56
    So if we say max split
    equals 1, then it
  • 4:56 - 4:59
    will only do one maximum split.
  • 4:59 - 5:03
    So this means we get two
    strings back in our list.
  • 5:03 - 5:05
    Number 10 is our split.
  • 5:05 - 5:06
    So this works the
    same as before.
  • 5:06 - 5:09
    But it starts splitting
    at the right side.
  • 5:09 - 5:13
    So if we use max split
    here as well and run this,
  • 5:13 - 5:17
    then it starts looking
    for the first split here.
  • 5:17 - 5:20
    And then we have
    this as one string
  • 5:20 - 5:22
    and this as one
    string in our list.
  • 5:22 - 5:24
    So if we run this, then
    this is our output.
  • 5:24 - 5:27
    Number 11 is the .join method.
  • 5:27 - 5:29
    So this is basically
    for the reversed way.
  • 5:29 - 5:32
    If we first have a list of
    strings and want to put this
  • 5:32 - 5:37
    back into one single string,
    then we can use .join with this
  • 5:37 - 5:38
    list of strings.
  • 5:38 - 5:41
    And it uses this
    string in between.
  • 5:41 - 5:43
    So if we run this,
    then it puts a dash
  • 5:43 - 5:46
    in between all the
    different items.
  • 5:46 - 5:49
    So here, if we use
    a space, then we
  • 5:49 - 5:51
    get this as a normal sentence.
  • 5:51 - 5:53
    Then we have upper
    lower and capitalize.
  • 5:53 - 5:55
    So this will put
    all the characters
  • 5:55 - 5:59
    into uppercase, all the
    characters into lowercase.
  • 5:59 - 6:02
    And capitalize means that
    only the first character
  • 6:02 - 6:05
    is capitalized and
    the rest is lowercase.
  • 6:05 - 6:07
    So if we run this, then
    this is our output.
  • 6:07 - 6:10
    15 and 16 are
    islower and isupper.
  • 6:10 - 6:14
    So islower returns true
    if all characters are
  • 6:14 - 6:17
    lowercase characters
    and false otherwise,
  • 6:17 - 6:19
    and the other way around
    for the isupper method.
  • 6:19 - 6:24
    So if we run this, so this
    will return true for islower.
  • 6:24 - 6:26
    And this will return
    true for isupper.
  • 6:26 - 6:29
    Then we have isalpha,
    isnumeric, and isalum.
  • 6:29 - 6:32
    So isalpha returns true if
    all characters in the string
  • 6:32 - 6:34
    are alphabetic.
  • 6:34 - 6:37
    isnumeric returns true if
    all characters in the string
  • 6:37 - 6:39
    are numeric, and
    isalum returns true
  • 6:39 - 6:42
    if we have either alphabetic
    or numeric characters.
  • 6:42 - 6:47
    So in this case, isalpha
    and isalum returns true.
  • 6:47 - 6:51
    And if we only use numbers,
    then isnumeric and isalum
  • 6:51 - 6:52
    returns true.
  • 6:52 - 6:56
    If we have both alphabetic
    and numeric numbers,
  • 6:56 - 6:59
    then only isalum returns true.
  • 6:59 - 7:02
    And for example, if we
    have another character
  • 7:02 - 7:06
    non-alphabetic, then none of
    those functions returns true.
  • 7:06 - 7:08
    Number 20 is the count
    function, so this
  • 7:08 - 7:11
    returns the number of
    non-overlapping occurrences
  • 7:11 - 7:14
    of this substring
    in this string.
  • 7:14 - 7:16
    So here it counts all the o's.
  • 7:16 - 7:18
    So if we run this, it returns 2.
  • 7:18 - 7:20
    If it does not find
    this substring,
  • 7:20 - 7:22
    for example, this
    then it returns 0.
  • 7:22 - 7:25
    Number 21 is the find function.
  • 7:25 - 7:29
    So this will return the first
    lowest index in the string
  • 7:29 - 7:31
    where it finds this substring.
  • 7:31 - 7:34
    So this will return the
    first index of this a.
  • 7:34 - 7:37
    So if we run this,
    then the index is 1.
  • 7:37 - 7:39
    And for example, if
    we use the substring,
  • 7:39 - 7:41
    then we get this string back.
  • 7:41 - 7:45
    Then, for example, if it
    does not find this string,
  • 7:45 - 7:47
    then it returns minus 1.
  • 7:47 - 7:48
    So be careful here.
  • 7:48 - 7:52
    And we can also use an
    optional start position.
  • 7:52 - 7:56
    So then it should start
    searching at this position.
  • 7:56 - 8:00
    So in this case, it starts
    looking at this position.
  • 8:00 - 8:04
    And then the next
    occurrence is at index 10.
  • 8:04 - 8:08
    So now if we use this substring,
    then we only get this part.
  • 8:08 - 8:09
    22 is rfind.
  • 8:09 - 8:11
    So this is the same as before.
  • 8:11 - 8:13
    But it starts searching
    from the right side.
  • 8:13 - 8:17
    So in this case, it returns the
    first occurrence from this side.
  • 8:17 - 8:19
    So in this case, it's also 10.
  • 8:19 - 8:20
    And then we get this substring.
  • 8:20 - 8:24
    23 and 24 are
    startswith and endswith.
  • 8:24 - 8:27
    So startswith returns
    true if this string starts
  • 8:27 - 8:30
    with this prefix, and
    endswith returns true
  • 8:30 - 8:33
    if this string ends
    with this suffix.
  • 8:33 - 8:36
    So if we run this, then both
    these methods return true.
  • 8:36 - 8:38
    Number 25 is the
    partition function.
  • 8:38 - 8:41
    This will split the string
    at the first occurrence
  • 8:41 - 8:45
    of this given string, and
    this will return a tuple
  • 8:45 - 8:49
    with three elements where the
    given string is in the middle,
  • 8:49 - 8:53
    and then we have the beginning
    of the string as first item
  • 8:53 - 8:56
    and the ending of the
    string as last item.
  • 8:56 - 8:59
    And if it does not
    find the given string,
  • 8:59 - 9:02
    then the whole string
    is the first item,
  • 9:02 - 9:05
    and the other two items
    are two empty strings.
  • 9:05 - 9:07
    Then we have center,
    ljust, and rjust.
  • 9:07 - 9:09
    And they come in handy
    when we want to format
  • 9:09 - 9:11
    our string in a certain way.
  • 9:11 - 9:16
    So center will put this string
    in another string of the given
  • 9:16 - 9:19
    length, and it uses this
    as the fill character.
  • 9:19 - 9:23
    So if we run this, then
    we have a centered string
  • 9:23 - 9:25
    of the length 30.
  • 9:25 - 9:28
    And on both sides, we
    have the fill character.
  • 9:28 - 9:31
    Then if we use ljust, then
    this will be the same,
  • 9:31 - 9:34
    but our string will
    be left justified.
  • 9:34 - 9:36
    So there on the
    left side, and then
  • 9:36 - 9:40
    we have all the fill characters
    until we reach width n.
  • 9:40 - 9:42
    And the same with rjust.
  • 9:42 - 9:45
    But here, our string
    will be right justified.
  • 9:45 - 9:47
    Number 29, our f string.
  • 9:47 - 9:49
    So this is not really
    a string method,
  • 9:49 - 9:52
    but this is a great new
    way since Python 3.6
  • 9:52 - 9:53
    to format a string.
  • 9:53 - 9:58
    And the syntax is very easy, we
    use an f and then our string.
  • 9:58 - 10:01
    And inside the string,
    we can use curly braces.
  • 10:01 - 10:05
    And inside the curly braces,
    we can use an expression.
  • 10:05 - 10:08
    For example, here we can
    simply put in a variable.
  • 10:08 - 10:11
    Or we can also use a
    mathematical expression
  • 10:11 - 10:13
    that will then be evaluated.
  • 10:13 - 10:17
    So now, if we run this,
    we get this string back,
  • 10:17 - 10:21
    and yeah, I think this is a
    very clear, easy, concise,
  • 10:21 - 10:24
    and more readable way
    to format your strings.
  • 10:24 - 10:27
    And it's also faster than
    other formatting methods.
  • 10:27 - 10:29
    Number 30 is the
    swap case method.
  • 10:29 - 10:32
    So this will swap all the cases.
  • 10:32 - 10:36
    So for a uppercase, it will then
    return a lowercase character
  • 10:36 - 10:37
    and then vice versa.
  • 10:37 - 10:40
    So if we run this, then
    this will be our result.
  • 10:40 - 10:42
    And number 31, zfill.
  • 10:42 - 10:46
    So this will return a
    string of the given length.
  • 10:46 - 10:50
    And it uses zeros to fill the
    string from the left side.
  • 10:50 - 10:53
    So if we run this, then
    this will be our string.
  • 10:53 - 10:58
    And also if we have a
    leading plus or minus sign,
  • 10:58 - 11:00
    then this will still
    be the first sign.
  • 11:00 - 11:05
    And only afterwards, it uses
    zeros to fill the string.
  • 11:05 - 11:09
    And this is also very useful
    if we have a number string
  • 11:09 - 11:11
    and want to format
    this in a certain way.
  • 11:11 - 11:12
    All right, that's it.
  • 11:12 - 11:13
    I hope you enjoyed
    this tutorial.
  • 11:13 - 11:15
    And if so, then please
    hit the Like button
  • 11:15 - 11:17
    and consider subscribing
    to the channel.
  • 11:17 - 11:18
    And then I hope to
    see you next time.
  • 11:18 - 11:20
    Bye.
Title:
31 ESSENTIAL Python String Methods
Description:

more » « less
Video Language:
English
Duration:
11:20

English subtitles

Revisions