Welcome to this tutorial on the data types strings in Python.
String is a sequence of Unicode characters in Unicode was introduced to include every character in all
languages and bring uniformity in encoding.
And strings in Python can contain as many characters as you wish.
The only limit is your machines memory resources Python does not have a character data type.
Like other programming languages.
A single character is simply a string which has a length of 1
and strings in Python can be created by enclosing inside single code or double code
so if you enclose this text in Edo single code or double code.
This is considered a string in python c is the case for this text now.
Consider this text.
It's a long work.
Now in order to be considered as a string in Python I'm going to enclose this text in single code.
It's a long walk so as but a discussion we have said that a string is something that can be enclosed
in a single code or double quote.
And once the Python interpreter read this text these string opens with a single code and the Python
interpreter assumes the next single code in the text is the closing delimiter.
The single code to your the final single code is then tree and causes a syntax error.
So let's see this in the Jupiter notebook.
So here we have our Jupiter notebook open and I have defined a string as these are one and the it points
to the text.
It's a long work and it is enclosed in single quotes Mullett executed this line and the code has been
interrupted with the syntax error Invalid syntax.
That's because the final single code is a straight and this is causing the syntax error.
So in order to avoid this error what we can do is we can enclose this string in double quotes so let
enclose the text in double codes and now print it out.
So there is no error here.
If there is a single code which is used within a string then we can delimit the string with double quotes
and if there are double codes that are used within the string.
In that case you can use the delimited single code
now coming back to strings we can access individual characters using indexing.
So what is indexing
so for example I've defined a string E with the text.
Now I would like to retrieve the third character in the string.
The third character is B.
So how do we do this.
This can be done using indexing
index start from zero and get incremented with each individual character in the string.
Now in order to leave the letter B the format is
E O squared.
Brackets and the index number or the letter B which is to
here it is the syntax for indexing is the variable which is pointing to the string happy and the index
number is enclosed in square brackets and your rewrite the index number within the square brackets.
Let's check indexing in the Jupiter book.
So I've defined a where you've been e happy which is a string executed.
Now I would like to index or retrieve the letter B which has an index to so within this grade bracket
I'm going to do but the number two
and to the letter B has been to.
And this is the syntax for indexing the variable a is pointing to the string happy and then the index
number is surrounded by square brackets in this retrieves the letter B.
If you try to access a character out of range then it will raise an index error.
So let's say variable is pointing to the string happy which contained so five characters let's say eight
or six let's see what the interpreter does so since there are only five characters in the string a it
has drawn the index into
and also the index must be an integer we can't used flawed or other types.
This will result in two or type error so giving plot numbers or any other data types results in type
error Python allows negative indexing for it sequences the index of minus one refers to the last item
minus 2 to the second last item and so on
say for example we want to retrieve the letter B again using negative indexing we can do minus three
in this grade bracket and at the same as e all do so let's check this in the Jupiter non book
let's do a full minus three so the same letter has been on the tree lit so to the next
letter B so that would be minus two.
This is about a nixing.