Transcription
Hello! In this lesson, we will talk about strings, learn about the functions available for working with them, and study the topic of indices and slices.
Before we begin, I would like to recommend the website etyprager.com. On this site, you will find code, homework assignments, and a lot of other useful information. The link to this lesson will be in the description of this video.
When working with strings, it is important to understand that any string is essentially a list where each element is a specific character from that string. Let's confirm this by creating a string. I will set the value to "etyprogi" and then I would like to display a specific character from this string. To do this, I need to refer to the string itself, and then I need to use square brackets to specify the index of the character I want.
For example, if I want to display the letter "e," I need to specify the index as 0 because indexing starts at 0. The first character will be at index 0, the second character at index 1, and so on. If I run this code, it will process correctly, and I will see the specific character displayed.
When you understand that any string is a list made up of various characters, you can use the same methods for strings that we previously used for lists. For instance, if I need to count the number of elements, or characters, in a string, I will use the same method we learned earlier, which is the `len()` method. After executing this piece of code, we get the number 8, which is the number of characters in the string.
In addition to `len()`, you can use other interesting methods. For example, the `count()` method can be used to find how many characters match a specific value. If I run this, it will return the number 1. If I check for a non-existent character, it will return 0.
So, when working with strings, you have access to many functions that exist for regular lists. Besides the functions available for lists, we also have special functions that exist exclusively for strings. Let's look at some of these functions.
For instance, if I want to display the entire string in uppercase, I can use the `upper()` function. If we run this, we notice that all characters are converted to uppercase.
Interestingly, there is also a function called `isupper()`, which returns either `True` or `False` depending on whether the entire string or a specific element is in uppercase. In our case, since the string is in lowercase, `isupper()` returns `False`.
There is also a function called `islower()`, which will return `True` because our string is in lowercase. Additionally, the `lower()` function simply converts all text to lowercase.
To verify this, I can input a character in uppercase, and after executing `lower()`, all characters will be converted to lowercase.
Another interesting function is `capitalize()`, which allows us to convert the first character of any word to uppercase while keeping all other characters in lowercase. If I run this, the first letter will be uppercase, while the others will be lowercase.
To find specific characters within a string, you can use the `find()` method. This method takes one or more characters, and if the character is found, it returns the index of that character. For example, if I input "b" into `find()`, it returns the number 2, which is the index of that character.
If I pass multiple characters, it will still return the index of the first character found. Thus, using the `find()` method, you can quickly locate specific characters or even substrings within a larger string.
In addition to `find()`, there is another method called `split()`. This method allows us to split a string by a specific character. For example, if I have a list where each element is separated by a comma, I can use `split()` to break the string into a list of elements.
If I run this code, I will see that a list has been created, consisting of three elements: "football," "basketball," and "skate." This method is incredibly useful. For instance, if we create a website where a user needs to input their hobbies separated by commas, we can use `split()` to break the string into individual elements.
We can also split by multiple characters. For example, if a user inputs hobbies separated by a comma and a space, we can specify that we will split the string by ", " (comma and space).
If I run this, nothing will change; we are just splitting the string by multiple characters.
Now, let's create a program where we will iterate through each element of the list and call the `capitalize()` method for each element. To implement this, we will create a loop.
I will define a new variable, say `element`, and then iterate through the list of hobbies. If I try to add a new value to the same element, I will notice that the list remains unchanged.
To modify the elements, we need to change the loop slightly. We will create a range, specifying the start and end of the range using the `range()` method. To determine the end number, I will use the `len()` method to get the length of the list.
Now, I can refer to the hobbies by their index and call `capitalize()` on each element. If I run this code, I will see that the list is now displayed correctly, with the first character of each element in uppercase and the rest in lowercase.
Next, I want to display a string instead of a list, where the string initially had incorrect capitalization. To combine the list back into a string, we will use the `join()` method.
First, I will create a variable called `result` and define a string that will be placed between the elements being combined. I will specify that we will use ", " (comma and space) as the separator.
Then, I will use `join()` and pass the list to it. Finally, I will print the resulting string. If I run this, I will get a regular string with all elements displayed correctly.
Now, let's touch on the topic of indices and slices. Indices and slices can be applied to both strings and lists. In the future, we will also study tuples, dictionaries, and sets, where you can also apply these indices and slices.
To start, let's create a string. I will write something like "football." I want to either refer to a specific character in this string or display a set of several characters using slices.
Slices allow us to refer to multiple indices at once. To do this, we specify the starting index and the ending index. For example, if I want to take the first four characters, I will start from index 0 and go up to index 4.
If I run this code, I will see that the first four characters are displayed. To select the last four characters, I can specify that I want to start from index 4.
If I run this, I will see that only the last two characters are displayed. If I want to display everything until the end of the string, I can omit the second index, and it will display everything until the end.
You can also use negative indices. For example, if I want to select the last three characters, I can specify the range from index -4 to the end.
Using slices, you can specify a range of elements you want to select. Additionally, you can specify a step. For example, if I want to start from index 0 and go up to the second-to-last character, I can specify a step of 2.
You can also use negative indices to reverse the order of elements. If I specify -1, the list will be reversed.
Now, let's look at an example with a list. I will create a list that can contain both numbers and strings.
If I want to display a specific element, I can refer to it by its index. For example, if I want to display the third element, I will refer to index 2.
If I want to display all elements starting from a specific element, I can use the slice notation. If I want to exclude the last element, I can use a negative index.
You can also specify a range using positive indices. For example, if I want to select elements from index 2 to index 5, I can do that as well.
You can easily find the needed characters or elements from lists, tuples, sets, and dictionaries using these indices and slices.
In conclusion, slices are also a great sorting tool. You can skip the first two values and display all elements starting from the first.
This lesson is coming to an end. I hope you enjoyed it. If so, don't forget to subscribe to the channel and join our social media. All links will be in the description of this video.
That's all for now. See you soon! Goodbye!