String is a sequence of characters enclosed by quotation marks : it can be any type either a single quotation mark or double quotation mark or it can be a triple quotation mark.
Let us see how we can define strings in python :
The output of the following code will be -
We can not define multiline strings enclosed by single or double quotation marks. If we do this then it will raise an error.
Multiline strings are defined by three quotation marks in python.
Let us understand this, how it works -
The resulting output of the following program will be -
Python Strings operate in the same manner as C character arrays - a Sequence of characters.
Let us understand this more clearly through an example -
This will be stored in memory in the form of sequential slicing, which means each character has a unique memory location/index. Python strings are arrays of bytes representing unicode characters.
In the above sequence, x has ‘c’ and x has ‘o’ and so on
Then, the output of the following code will be -
As we can see in the above, python string follows negative indexing as well. It starts from the end of the string.
The output of the following program will be -