Python for Beginners - 04 | Built-in Data Types | [Strings - Part 1]

Published: 01 July 2024
on channel: E-Skillation
42
8

Welcome to E-Skillation. In our introduction to Python for beginners we discussed data types, we learnt Numbers. In this video let's understand Strings in more detail. Strings as the name suggests are nothing but sequence of textual data stored as bytes. Which means it is splitting each character to a byte and storing at different locations. For example, I will assign a text “Python for Beginners” to a variable s here, and we can fetch the characters using indexes. As in, I can now fetch the 2nd or the 3rd character from this string using index. That’s because it is stored as a sequence. Let us check the location of the string and each item to confirm the same. If you look at these memory locations, there is 1 location which is pointing to our actual string and each item in the string are stored at different locations. Here we can assume that the string location is pointing to each character’s memory location rather than storing the string itself. These type of data are called sequence data types. Other sequence data types includes lists and tuples which we will discuss in our upcoming videos.

Now let’s deep dive to understand it more. [Mutability ]
Will start by printing all the character’s location along with the character itself. If you notice, same characters in the string are not stored separately instead its just pointing to same location. In this example, the spaces are pointing to the same location, letter n in the word beginner is occurring twice and hence saved in the same location.
Now we will try updating this string and check the locations again. Let me update it by replacing spaces with an underscore. For which I will use a built-in string function called replace and pass arguments of string that need to be replaced which is space in this case and new string to be replaced by which is an underscore. By updating the same string variable and checking the location as earlier, the location of the string is changing which means it is getting created as a new object and not overwriting the existing. However, those items or characters that are not changed will still be in the same location and only the updated items or the new characters are getting stored in a new location. In other words that is also getting created as a new object. Hence, Strings are immutable.
Let us look at it visually to understand indexes, memory locations and mutability of strings much easier, strings are stored as sequences and each item is stored separately. Similar characters are stored in the same location. The actual string is pointing to these locations. And when there is a change in the value, it will create a new object of the string and internally it will keep the existing items and create new objects for new items. As in our previous example where we replaced spaces with an underscore as underscore was not a part of our string earlier, it will point to a new location. In another example, let us say we replace letter s to a space. Because space was already present in our initial string where it pointed to location 7, now the replaced value will start pointing to location 7 itself instead of creating a new object. Also, it is case sensitive. For example, lets replace letter t with a capital P and letter h with a small p. As we already have a capital P at location 1, there is no need to create a new object for capital P. however, for the smaller case it will go ahead and create a new object and again the string location will change as we made some changes to the string.

Now that we understood mutability of strings and how it is getting saved in memory lets look at different ways of creating strings. [String Literals]
It can be created by using single or double quotes, we can embed the phrase Python for Beginners either between a single or a double quote too in our example and it still works. That is because we can embed the other type of quotes inside the text if needed. For example, if we have a phrase “I’ve been learning python from E-Skillation”, we can create this string with single quotes by using double quotes on either sides. Similarly, another example where the phrase could be ‘he said, “I am learning python from E-Skillation”’. Well, it throws a Syntax error. Now you might think what if I have sentences with both quotations. For which we can always use escape character to consider the quote inside the string which is a backslash just before the quote. Then the type of quotes outside does not matter.
There is another way of writing textual data by using triple quotes on either sides. Doing which we can include new lines without any escape characters. These triple quotes can be either single or double.

In our next video let us continue string literals along with escape characters. Subscribe to my channel to get notifications on upcoming videos.


On this page of the site you can watch the video online Python for Beginners - 04 | Built-in Data Types | [Strings - Part 1] with a duration of hours minute second in good quality, which was uploaded by the user E-Skillation 01 July 2024, share the link with friends and acquaintances, this video has already been watched 42 times on youtube and it was liked by 8 viewers. Enjoy your viewing!