< Return to Video

How to Use Strings in Python - Python Tutorial for Beginners

  • 0:00 - 0:03
    [MUSIC PLAYING]
  • 0:03 - 0:04
  • 0:04 - 0:06
    MOSH HAMEDANI: So here we
    have this course variable
  • 0:06 - 0:08
    set to Python programming.
  • 0:08 - 0:10
    As I told you before,
    whenever you work with text,
  • 0:10 - 0:13
    you should surround
    your text with quotes.
  • 0:13 - 0:16
    You can either use double
    quotes or single quotes.
  • 0:16 - 0:18
    That's more of a
    personal preference,
  • 0:18 - 0:21
    but quite often we
    use double quotes.
  • 0:21 - 0:24
    We also have triple
    quotes, and we use
  • 0:24 - 0:26
    them to format a long string.
  • 0:26 - 0:31
    For example, if you have, let's
    say, a variable message, that
  • 0:31 - 0:36
    is the message we want to
    include in the body of an email.
  • 0:36 - 0:39
    You can use triple quotes
    to format it like this--
  • 0:39 - 0:42
    Hi, John.
  • 0:42 - 0:45
    This is Mosh from
    codewithmosh.com--
  • 0:45 - 0:47
    blah, blah, blah.
  • 0:47 - 0:50
    So that's when we
    use triple quotes.
  • 0:50 - 0:52
    Now we don't need
    in this lecture.
  • 0:52 - 0:53
    So delete.
  • 0:53 - 0:57
    Let me show you a few useful
    things you can do with strings.
  • 0:57 - 1:01
    First of all, we have this
    built-in function in Python
  • 1:01 - 1:03
    for getting the
    lengths of strings.
  • 1:03 - 1:05
    What is a function?
  • 1:05 - 1:08
    A function is basically
    a reusable piece of code
  • 1:08 - 1:10
    that carries out a task.
  • 1:10 - 1:14
    As a metaphor, think of the
    remote control of your TV.
  • 1:14 - 1:17
    On this remote control, you have
    buttons for different functions,
  • 1:17 - 1:21
    like turn on, turn off,
    change the channel, and so on.
  • 1:21 - 1:24
    These are the built-in
    functions in your TV.
  • 1:24 - 1:27
    In Python and many other
    programming languages,
  • 1:27 - 1:29
    we have the exact same concept.
  • 1:29 - 1:31
    So we have functions
    that are built
  • 1:31 - 1:33
    into the language
    and the platform.
  • 1:33 - 1:36
    You can reuse these functions
    to perform various tasks.
  • 1:36 - 1:39
    So here we can use the
    built-in len function
  • 1:39 - 1:41
    to get the length
    of a string, which
  • 1:41 - 1:44
    means the number of
    characters in that string.
  • 1:44 - 1:46
    Now whenever you want
    to use a function,
  • 1:46 - 1:49
    you should use parentheses.
  • 1:49 - 1:53
    Now we say we're calling this
    function, which basically means
  • 1:53 - 1:54
    we are using this function.
  • 1:54 - 1:58
    Now some functions
    take additional data,
  • 1:58 - 2:00
    which we refer to as arguments.
  • 2:00 - 2:03
    These arguments are
    inputs to these functions.
  • 2:03 - 2:08
    So this len function takes
    an input or an argument.
  • 2:08 - 2:11
    Here we pass our
    course variable.
  • 2:11 - 2:15
    And this will return the number
    of characters in this string.
  • 2:15 - 2:19
    So let's print that
    and see what we get.
  • 2:19 - 2:20
    Run the program.
  • 2:20 - 2:25
    We get 18 because we
    have 18 characters here.
  • 2:25 - 2:27
    Let's look at another example.
  • 2:27 - 2:30
    If you want to get access
    to a specific character
  • 2:30 - 2:33
    in this string, you use the
    square bracket notation.
  • 2:33 - 2:37
    So here we add course
    square brackets.
  • 2:37 - 2:41
    To get the first character,
    you use the index 0.
  • 2:41 - 2:44
    So in Python, like
    many other languages,
  • 2:44 - 2:47
    strings are 0
    indexed, which means
  • 2:47 - 2:52
    the index of the first character
    or the first element is 0.
  • 2:52 - 2:59
    So now when we print
    this, we'll get p.
  • 2:59 - 3:05
    Now you can also use a
    negative index like minus 1.
  • 3:05 - 3:06
    What does that mean?
  • 3:06 - 3:11
    Well, if 0 represents
    the first character here,
  • 3:11 - 3:15
    what do you think
    negative 1 represents?
  • 3:15 - 3:17
    That takes us back to
    the end of the string.
  • 3:17 - 3:20
    So that returns
    the first character
  • 3:20 - 3:21
    from the end of the string.
  • 3:21 - 3:23
    Let's run this program.
  • 3:23 - 3:26
    You will see we'll get g.
  • 3:26 - 3:28
    There you go.
  • 3:28 - 3:32
    Using a similar syntax,
    you can slice strings.
  • 3:32 - 3:33
    Let me show you.
  • 3:33 - 3:37
    So I'm going to duplicate this
    line and remove negative 1.
  • 3:37 - 3:40
    Now let's say we want to
    extract the first three
  • 3:40 - 3:42
    characters in this string.
  • 3:42 - 3:44
    So here we need two indexes--
  • 3:44 - 3:48
    the start index
    colon the end index.
  • 3:48 - 3:51
    So this will return
    a new string that
  • 3:51 - 3:55
    contains the first three
    characters in this course
  • 3:55 - 3:55
    variable.
  • 3:55 - 3:58
    That will be p, y, and t.
  • 3:58 - 4:02
    So the index of these
    characters are 0, 1, and 2.
  • 4:02 - 4:08
    So that means the character at
    the end index is not included.
  • 4:08 - 4:12
    Let's run the program and make
    sure we get the right result.
  • 4:12 - 4:12
    There you go--
  • 4:12 - 4:14
    Pyt.
  • 4:14 - 4:18
    Now, what if we don't
    include the end index?
  • 4:18 - 4:19
    What do you think
    we're going to get?
  • 4:19 - 4:21
    It's common sense.
  • 4:21 - 4:24
    We start from index
    0 and go all the way
  • 4:24 - 4:26
    to the end of the string.
  • 4:26 - 4:28
    So this will return
    a new string that
  • 4:28 - 4:31
    is exactly the same as
    the original string.
  • 4:31 - 4:34
    Let's take a look.
  • 4:34 - 4:37
    So we get Python programming.
  • 4:37 - 4:40
    Now, what if we don't
    include the start index
  • 4:40 - 4:43
    but include the end index.
  • 4:43 - 4:45
    What do you think
    we're going to get?
  • 4:45 - 4:46
    Once again, it's common sense.
  • 4:46 - 4:49
    So by default, Python
    will put 0 here.
  • 4:49 - 4:52
    So it will start from the
    beginning of the string.
  • 4:52 - 4:56
    So when I run this program, we
    should get Pyt one more time.
  • 4:56 - 4:58
    There you go.
  • 4:58 - 5:01
    And finally, as
    the last example,
  • 5:01 - 5:04
    if we don't include the
    start and the end index,
  • 5:04 - 5:07
    this will return a copy
    of the original string.
  • 5:07 - 5:09
    Let's look at this.
  • 5:09 - 5:13
    So we get Python programming.
  • 5:13 - 5:15
    Now you don't have to
    memorize any of these.
  • 5:15 - 5:18
    Just remember we
    use the len function
  • 5:18 - 5:19
    to get the length of a string.
  • 5:19 - 5:22
    We use bracket
    notation to get access
  • 5:22 - 5:25
    to a specific element
    or a specific character.
  • 5:25 - 5:28
    And we use this notation
    to slice a string.
  • 5:28 - 5:31
  • 5:31 - 5:34
    [MUSIC PLAYING]
  • 5:34 - 5:36
  • 5:36 - 5:39
    So we have this string
    here, Python programming.
  • 5:39 - 5:42
    Now let's say we want
    to put a double quote
  • 5:42 - 5:44
    in the middle of this string.
  • 5:44 - 5:45
    There is a problem here.
  • 5:45 - 5:49
    Python interpreter
    sees this second string
  • 5:49 - 5:51
    as the end of the string.
  • 5:51 - 5:55
    So the rest of the code is
    meaningless and invalid.
  • 5:55 - 5:57
    How do we solve this problem?
  • 5:57 - 5:58
    Well, there are two ways.
  • 5:58 - 6:03
    One way is to use single
    quotes for our string,
  • 6:03 - 6:06
    and then we can
    use a double quote
  • 6:06 - 6:07
    in the middle of the string.
  • 6:07 - 6:09
    But what if, for
    whatever reason,
  • 6:09 - 6:12
    perhaps for being
    consistent in our code,
  • 6:12 - 6:15
    we decided to use double quotes?
  • 6:15 - 6:17
    How can we add
    another double quote
  • 6:17 - 6:19
    in the middle of this string?
  • 6:19 - 6:24
    Well, we can prefix
    this with a backslash.
  • 6:24 - 6:28
    Backslash in Python strings
    is a special character.
  • 6:28 - 6:31
    We have a jargon for that
    called escape character.
  • 6:31 - 6:35
    We use it to escape
    the character after.
  • 6:35 - 6:37
    Let me show you what I mean.
  • 6:37 - 6:43
    So let's print this course
    and run this program.
  • 6:43 - 6:44
    What's going on here?
  • 6:44 - 6:46
    We don't have the
    backslash because we
  • 6:46 - 6:49
    use that to escape
    this double code
  • 6:49 - 6:52
    and basically display it here.
  • 6:52 - 6:55
    So backslash is an
    escape character.
  • 6:55 - 6:59
    And backslash double quote
    is an escape sequence.
  • 6:59 - 7:02
    In Python strings, we have
    a few other escape sequences
  • 7:02 - 7:04
    that you should be aware of.
  • 7:04 - 7:05
    Let me show you.
  • 7:05 - 7:10
    So in Python, we use a hash
    sign to indicate a comment.
  • 7:10 - 7:13
    A comment is like additional
    note that we add to our program.
  • 7:13 - 7:17
    It's not executed by
    Python interpreter.
  • 7:17 - 7:19
    So here are the
    escape sequences.
  • 7:19 - 7:22
    You have seen
    backslash double quote.
  • 7:22 - 7:26
    We also have backslash
    single quote.
  • 7:26 - 7:30
    So we can use that to
    add a single quote here.
  • 7:30 - 7:31
    Let's run the program.
  • 7:31 - 7:32
    Here it is.
  • 7:32 - 7:34
    Beautiful!
  • 7:34 - 7:37
    We also have double backslash.
  • 7:37 - 7:41
    So if you want to include a
    backslash in your strings,
  • 7:41 - 7:43
    you should prefix it
    with another backslash.
  • 7:43 - 7:45
    Let me show you.
  • 7:45 - 7:47
    So when we run this,
    we get Python one
  • 7:47 - 7:50
    backslash programming.
  • 7:50 - 7:55
    And finally, we have backslash
    n, which is short for new line.
  • 7:55 - 8:02
    So now if I add a backslash
    n here, see what we get.
  • 8:02 - 8:04
    We get a new line after Python.
  • 8:04 - 8:08
    So programming will end
    up on the second line.
  • 8:08 - 8:10
    So these are the escape
    sequences in Python.
  • 8:10 - 8:14
  • 8:14 - 8:17
    [MUSIC PLAYING]
  • 8:17 - 8:18
  • 8:18 - 8:21
    Here we have two
    variables-- first and last.
  • 8:21 - 8:24
    Let's say we want to print
    my full name on the console.
  • 8:24 - 8:30
    So we can define another
    variable, full, set it to first.
  • 8:30 - 8:33
    Then concatenate
    it with a space.
  • 8:33 - 8:37
    And one more time
    concatenate it with last.
  • 8:37 - 8:43
    Now when we print full, we get
    my full name on the console.
  • 8:43 - 8:44
    Beautiful!
  • 8:44 - 8:49
    Now this approach of using
    concatenation to build a string
  • 8:49 - 8:52
    is OK, but there is a
    better and newer approach.
  • 8:52 - 8:55
    We can use formatted strings.
  • 8:55 - 8:58
    So here we can set
    full to this string
  • 8:58 - 9:03
    and prefix it with an f, which
    can be lowercase or uppercase.
  • 9:03 - 9:07
    This formatted string
    doesn't have a constant value
  • 9:07 - 9:09
    like these two strings here.
  • 9:09 - 9:13
    It's actually an expression that
    will be evaluated at runtime.
  • 9:13 - 9:17
    So here we want to
    add our first name.
  • 9:17 - 9:22
    We use curly braces to print
    the value of the first variable.
  • 9:22 - 9:26
    After that, we add
    a space, and then we
  • 9:26 - 9:30
    add curly braces one more
    time to print the last name.
  • 9:30 - 9:34
    So at runtime, this
    expression will be evaluated.
  • 9:34 - 9:37
    What we have in
    between curly braces
  • 9:37 - 9:40
    will be replaced at runtime.
  • 9:40 - 9:43
    Now let's run this
    program one more time.
  • 9:43 - 9:46
    We get the exact same
    result. Just be aware
  • 9:46 - 9:49
    that you can put any
    valid expressions
  • 9:49 - 9:51
    in between curly braces.
  • 9:51 - 9:55
    So earlier, you learned about
    the built-in len function.
  • 9:55 - 10:00
    We can call len here to get
    the length of this string.
  • 10:00 - 10:04
    Let's run this program
    one more time so we get 4.
  • 10:04 - 10:09
    We can also replace last with an
    expression like this, 2 plus 2.
  • 10:09 - 10:11
    Let's run this program.
  • 10:11 - 10:13
    We get 4 and 4.
  • 10:13 - 10:16
    So when using
    formatted strings, you
  • 10:16 - 10:19
    can put any valid expressions
    in between curly braces.
  • 10:19 - 10:24
  • 10:24 - 10:27
    [MUSIC PLAYING]
  • 10:27 - 10:28
  • 10:28 - 10:31
    In this lecture, we're going to
    look at a few useful functions
  • 10:31 - 10:33
    available to work with strings.
  • 10:33 - 10:37
    So earlier, you learned about
    this built-in len function.
  • 10:37 - 10:39
    This function is
    general purpose.
  • 10:39 - 10:41
    So it's not limited to strings.
  • 10:41 - 10:43
    Later I will show you
    how to use this function
  • 10:43 - 10:45
    with other objects.
  • 10:45 - 10:48
    But in Python, we have
    quite a few functions
  • 10:48 - 10:50
    that are specific to a strings.
  • 10:50 - 10:51
    Let me show you.
  • 10:51 - 10:57
    So here, if we type
    course., see, all these
  • 10:57 - 11:00
    are functions
    available on strings.
  • 11:00 - 11:05
    Now, in precise terms, we refer
    to these functions as methods.
  • 11:05 - 11:08
    This is a term in
    object-oriented programming
  • 11:08 - 11:11
    that you will learn about
    later in the course.
  • 11:11 - 11:13
    For now, what I want
    you to take away
  • 11:13 - 11:16
    is that everything in
    Python is an object,
  • 11:16 - 11:19
    and objects have
    functions we call methods
  • 11:19 - 11:23
    that we can access
    using the dot notation.
  • 11:23 - 11:25
    So here course is an object.
  • 11:25 - 11:29
    We use the dot notation to
    access its functions or more
  • 11:29 - 11:31
    accurately methods.
  • 11:31 - 11:33
    Let's take a look at a
    few of these methods.
  • 11:33 - 11:37
    We have upper to convert
    a string to uppercase.
  • 11:37 - 11:41
    Now, let's print this
    and run the program.
  • 11:41 - 11:43
    Here is what we get.
  • 11:43 - 11:44
    Beautiful!
  • 11:44 - 11:46
    Now note that the methods
    that you call here
  • 11:46 - 11:48
    return a new string.
  • 11:48 - 11:51
    So the original string
    is not affected.
  • 11:51 - 11:51
    Let me show you.
  • 11:51 - 11:54
    So print course.
  • 11:54 - 11:56
    Run the program one more time.
  • 11:56 - 11:57
    Look.
  • 11:57 - 11:59
    This is our original string.
  • 11:59 - 12:04
    So course.upper returns a
    new string, a new value.
  • 12:04 - 12:11
    We can store it in a variable
    like course_capital like this.
  • 12:11 - 12:13
    Now to keep this demo
    simple and consistent,
  • 12:13 - 12:18
    I'm going to revert this back
    and use a print statement.
  • 12:18 - 12:23
    We also have the lower method to
    convert a string to lowercase.
  • 12:23 - 12:27
    We also have title,
    which will capitalize
  • 12:27 - 12:30
    the first letter of every word.
  • 12:30 - 12:36
    So if our string was like this,
    when we call the title method,
  • 12:36 - 12:41
    we get Python programming
    as you see here.
  • 12:41 - 12:44
    Another useful method
    is strip, and we
  • 12:44 - 12:47
    use it to trim any
    whitespace at the beginning
  • 12:47 - 12:48
    or end of a string.
  • 12:48 - 12:52
    This is particularly useful when
    we receive input from the user.
  • 12:52 - 12:53
    Let me show you.
  • 12:53 - 12:59
    So let's imagine the user
    entered a couple of white spaces
  • 12:59 - 13:01
    at the beginning of this string.
  • 13:01 - 13:06
    When we call course.strip, those
    white spaces will be removed.
  • 13:06 - 13:08
    Take a look.
  • 13:08 - 13:10
    So note that in the
    first three examples,
  • 13:10 - 13:12
    we have those white spaces.
  • 13:12 - 13:15
    But in the last
    one, it is removed.
  • 13:15 - 13:18
    So a strip removes the white
    space from both the beginning
  • 13:18 - 13:20
    and end of a string.
  • 13:20 - 13:24
    We also have lstrip, which
    is short for Left Strip,
  • 13:24 - 13:27
    and rstrip, which is
    short for Right Strip.
  • 13:27 - 13:30
    So it will remove the white
    space from the end of a string.
  • 13:30 - 13:33
    If you want to get the
    index of a character
  • 13:33 - 13:36
    or a sequence of
    characters in your string,
  • 13:36 - 13:38
    you should use the find method.
  • 13:38 - 13:39
    Let me show you.
  • 13:39 - 13:42
    So course.find.
  • 13:42 - 13:47
    So as an argument, here
    we pass another string.
  • 13:47 - 13:51
    We can pass a character
    or a series of characters.
  • 13:51 - 13:55
    Let's find the index of pro.
  • 13:55 - 13:56
    Run the program.
  • 13:56 - 13:58
    So the index of pro is 9.
  • 13:58 - 14:02
    So if you start from 0
    here all the way to 9,
  • 14:02 - 14:06
    this is the index of pro.
  • 14:06 - 14:08
    Now, as I told
    you before, Python
  • 14:08 - 14:10
    is a case-sensitive language.
  • 14:10 - 14:13
    So if I pass a capital
    P here, obviously we
  • 14:13 - 14:17
    don't have these exact
    characters in our string.
  • 14:17 - 14:20
    So let's see what we get.
  • 14:20 - 14:21
    We get negative 1.
  • 14:21 - 14:26
    That means this string was not
    found in the original string.
  • 14:26 - 14:28
    Another useful
    method is replace.
  • 14:28 - 14:32
    So we call replace.
  • 14:32 - 14:34
    With this, we can
    replace a character
  • 14:34 - 14:37
    or a sequence of characters
    with something else.
  • 14:37 - 14:42
    So let's say we want to replace
    all lowercase p's with j.
  • 14:42 - 14:47
    With this, we get
    jython jrogramming,
  • 14:47 - 14:49
    whatever that means.
  • 14:49 - 14:50
    And finally, if
    you want to check
  • 14:50 - 14:55
    for the existence of a character
    or a sequence of characters
  • 14:55 - 14:58
    in your string, you can
    use the in operator.
  • 14:58 - 14:59
    Let me show you.
  • 14:59 - 15:07
    So print, rewrite an expression
    like this-- pro in course.
  • 15:07 - 15:08
    So this is an expression.
  • 15:08 - 15:10
    As I told you
    before, an expression
  • 15:10 - 15:13
    is a piece of code
    that produces a value.
  • 15:13 - 15:18
    So this expression checks to
    see if we have pro in course.
  • 15:18 - 15:21
    The difference between this
    expression and calling the
  • 15:21 - 15:24
    find method is that
    the find method returns
  • 15:24 - 15:28
    the index of these
    characters in our string,
  • 15:28 - 15:31
    whereas this expression
    returns a Boolean,
  • 15:31 - 15:32
    so it's a true or false.
  • 15:32 - 15:33
    Let me show you.
  • 15:33 - 15:35
    So run the program.
  • 15:35 - 15:39
    We get the Boolean true.
  • 15:39 - 15:42
    And finally, we have
    the not operator.
  • 15:42 - 15:45
    And we use that to see
    if our string does not
  • 15:45 - 15:48
    contain a character or a
    sequence of characters.
  • 15:48 - 15:57
    So let's change this
    to swift not in course.
  • 15:57 - 16:00
    When this expression
    is evaluated,
  • 16:00 - 16:01
    what do you think
    we're going to get?
  • 16:01 - 16:04
    Well, we don't have
    swift in this string,
  • 16:04 - 16:08
    so not in will return true.
  • 16:08 - 16:10
    Let's take a look.
  • 16:10 - 16:12
    There you go.
  • 16:12 - 16:15
    So these are the
    useful string methods.
  • 16:15 - 16:16
    Next, we'll look at numbers.
  • 16:16 - 16:21
  • 16:21 - 16:21
    Hi, guys.
  • 16:21 - 16:23
    Thank you for watching
    this tutorial.
  • 16:23 - 16:25
    My name is Mosh
    Hamedani, and I have
  • 16:25 - 16:27
    tons of tutorials like
    this for you on my channel.
  • 16:27 - 16:29
    So be sure to subscribe!
  • 16:29 - 16:32
    And also please like
    and share this video.
  • 16:32 - 16:34
    If you want to learn
    Python properly
  • 16:34 - 16:37
    from scratch with depth, I have
    a comprehensive Python tutorial
  • 16:37 - 16:38
    for you.
  • 16:38 - 16:39
    The link is below this video.
  • 16:39 - 16:41
    So click the link
    to get started.
  • 16:41 - 16:44
    Thank you, and have
    a fantastic day!
  • 16:44 - 16:45
Title:
How to Use Strings in Python - Python Tutorial for Beginners
Description:

more » « less
Video Language:
English
Duration:
16:46

English subtitles

Revisions