Engineering Python 05A: String Basics, Indexing, and Slicing

Welcome to Engineering Python. This is a Python programming course for engineers. In this video, I’ll talk about the basics
of strings and how to access string elements using indexing and slicing. In Python, a string is a sequence of characters
enclosed within a pair of double or single quotation marks. This s is a string. It contains two words, Hello World, and a
space. They are enclosed with double quotation marks. We can print the string using the print function. We can check the data type of variable s. It’s a string. A pair of quotation marks, double or single,
with nothing inside, is an empty string, like this one. In this statement, we will show the user a
message, What’s your name? The user’s input, which is a string, will
be given to this variable name. For example, the user’s input is Jack Sparrow. Now we can say hi to this user by joining
two strings together. The first string is hi and the second is the
name. They are separated by a comma in the print
function. Note that curved quotation marks won’t work,
no matter single or double. Like this print Hello World or this one. Python will show you there is an invalid character. We can use quotation marks inside quotation
marks. For example, He said, “You look young.” We have single quotation marks inside double
quotation marks. Or this one, double quotations marks inside
single quotation marks. They are both acceptable because there is
no confusion about the meaning. We can also use double inside double and single
inside single. But because they may cause confusion, we have
to use a backslash to escape an inner quotation mark. Like this or this. Another way is to use a triple quoted string. In this example, there are three double quotation
marks at the beginning and three at the end. So, everything in between will be printed
verbatim. Here, there are three single quotation marks
at the beginning and three at the end. It also works well. String indexing and slicing are two very important
concepts. We will talk about indexing first. We can use indexing to access individual characters
in a string. If we count from left to right, string indexes
start with 0 and increase. If we go from right to left, they start with
-1 and decrease. Let’s use the string variable name as an example. name contains a string, Jack Sparrow. This image illustrates the indexing concept
very clearly. In this statement, name is followed by a pair
of brackets, and a zero is inside the brackets. It means we are accessing the zeroth element
of this string. The zeroth element is just a letter J.
This statement prints three letters in the string. The zeroth letter, which is J, the third letter,
which is k, and the -11th letter, which is a. Now let’s talk about slicing. We can use slicing to access a consecutive
sequence of characters or a substring in a string. This is the syntax. The string’s name followed by a pair of brackets. Inside the brackets, we have a start position
and an end position. Both start and end must be integer numbers. What it does is that it produces a substring
beginning at start and runs up to but doesn’t include end. Let’s still use Jack Sparrow as an example. This statement accesses the substring of name
from the 5th character, which is S, to the 100th character, which is greater than the
highest index 11. Then we ignore the non-existing characters
and only go up to 11. The output is Sparrow. This statement doesn’t define a starting point. So, it accesses the substring from 0 to 5,
but not including 5. So, the output is Jack space. This statement doesn’t define an end. So, it accesses the substring from 5 to the
very end of the string. This one will access a substring from -9 to
-6 but not including -6. So, the output is k space S.
This one will access a substring from -3 to the very end. So the output is row. This one will access a substring from the
very start to -6 but not including -6. The output is Jack space S.
This one uses a mix of both positive and negative indexes. It accesses a substring from positive 2 to
-6. ck space S.
This one, from 9 to 5, not including 5. Because the start and end are both positive
numbers and the end is less than the start, we cannot go backwards. The result is an empty string. The last one, from 3 to 3, but not including
3. This is also an empty string. Okay, I just talked about the basics of strings,
string indexing, and string slicing in this video. The course materials are available on YouTube
and GitHub. You can watch the course videos in sequence. If you like this video, please subscribe and
share. I’m Yong Wang. Thanks for watching.

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

One Comment

  • Yong Wang says:

    Hi Guys, please comment and let me know what you think about this Engineering Python open course. If you enjoy the video, please subscribe and share with your friends. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *