Now let's move on to strings and python.
So how can we pass a string in a python if I just write as the python tutorial and executed
I'm getting an error because we need to pass strings and double quotes or single chords so we can write
double chords as deep Python tutorials
here.
We use double quotation marks.
You can also use single quotes to pass a string but sometimes your string may contain single quotes
in between.
So therefore it is recommended to use double quotes while passing the strings
just like numbers.
We can also assign this thing to some variable so we can write SB one here SDR one is over variable
and we are assigning value asleep by tend to Dodi Al
no my SDR 1 variable contains this value.
Now if we want to display the value of our SDR one variable I will just write SDR one and then execute
this Come on.
You can see we are getting our output and you can also notice that at speed by 10 tutorial is coming
in quotation marks.
There is also a separate print function and button to print the output to print output will write print
and in brackets will write SDR 1
you can now notice that there is no quotation marks and now the values are printed and they are not
a part of 0 4.
You can see when we just executed as earlier when we were getting the content of this variable in the
form of output.
You can see this output returning it.
But when we are printing it by then is actually printing it.
And this is not a part of our
Python even provides formatting options with this print function so suppose I wanted to use some variable
inside my brain come on I can do that by using green formatting for example and then what previous example
in the phrase as deep Python tutorial I wanted to pass variable X which will take value by 10 or are
depending the value
so if my axis python I wanted to print as true by standard Oriel if my X is odd I wanted to print as
the r tutorial or if X is a school I wanted to print as B and school tutorial so let's create this variable
X and assign it value add
notice that we are using double quotes to assign value to this variable
we will first rate print
and then records will put quotation marks and will pass the update string which is s.t. and then since
I won my second word too with this variable X I will I will give complete records and my third word
is upon strength which is tutorial.
So I will write tutorial.
Now I want to tell python what goes inside this go live record.
So after this print come on I will put a dot and write format and in parenthesis I can write my variable
name which is X
let's run this we are getting this edit because dot format should be inside the print come on.
So we just copy paste it
yeah.
And now if we execute it you can see we are getting as the odd tutorial if I change my value of x to
skew it.
And then again then this spring come on you can see no sense of what X is askew in the output is segment
based SD is squarely custodian.
So what's happening here is the live record in print come on titles Python to take the value of variable
that is mentioned in the dot format parenthesis if I want I can also pass toward more than two variables.
This index will remain the same in that case.
And you have to mention the variable names and not form it in the order you want them to be filled in
the college record.
So if we just write print
and index it we'll write s.t.
then college records yet our variable X will come then after this tutorial
and then another can leave record
and in dot we can write X and the variable we created earlier that was very when if we executed this
you can see what where one variable was containing 14 so what printed string it a city school tutorial
for being now let's discuss some important functions related to strings.
The first function is land function.
It helps us to find the length of our string the syntax of Len.
Function as a.
And now if we want to check the length of our we one variable we can just mention SDR one inside parenthesis
after length.
You can see the length of our variable as are one is 18 also known that is spaces also character so
this length 18 is count of all the characters including spaces and the penetration marks as well another
string function which we frequently used is replaced function.
You must have used replace function involved or not bank where you search the word and then replace
that word with a new word replace function and by Python bucks.
Exactly the same.
You have to give two words
first the word you want to search.
And second the vote you want to replace it with.
So let's again print over SDR one variable
right now.
My SDR 1 contains a steep Python tutorial if I want to replace this word python with as school I will
write SB 1 not replace and then record for the word I want to replace that is Python.
Also remember that Python is case sensitive.
Language so I have to pass python in lowercase since my variable contains python and lowercase i want
to replace this variable with a script.
So after a comma I will write a script.
You can see that the word by 10 has now been replaced with a school I can also save this value in another
variable.
So let's see if this value in another variable called X
I will copy paste so function
if I bring Dex
and you get this value as the school.
Claudia.
Now let's move on to a string indexes and string slicing.
You can see a string is a collection of several individual characters.
For example in our variable X as is a single character B is a single character and our whole string
is a collection of such characters.
This characters are present at a specified position they know what a string.
So for example as is present at the first politician B is present at the second position.
And similarly we have a space and the third position
we can extract this individual characters from our variable by specifying their positions.
This concept of extracting characters by their position is known this string indexing of all is String
slicing.
One thing to note here is that in Python indexing starts with zero
so a word character as is presented 0 to index B is present at first index and space is presented to
index so if I want to extract the character which is present at the first index I will just straight
up what a variable name which is x and then a squared record.
I will mention the index which is 1
if we execute this we will get B as I would output since B was present at the first index position is
different from index the position of B is two but since the index starts from 0 0 it as was presented
C both index and nobody was present at first index
I can also mention multiple index.
So if I want all the characters which are present from index to the index 5 I will write X and then
a squared record I will write to and then I will provide the colon symbol and then I will write 5.
This means that I want all characters which are present between index 2 and 5 notice that our first
character is a space where the second character is as another third character is skewed.
We are getting all the characters that are present at index 2 index 3 and index for we are not getting
our index 5 variable because the second argument is always excluded while slicing the string using indexes
so too.
Column 5 means character presented index 2 3 and 4
similarly if we write X bracket for colon 6 it will return the characters which are present at index
4 and 5 index 6 will be excluded from all the selection indexes also supports negative argument negative
it for counting from the backside of this string so minus 1 will be the last character of string minus
2 will be the second last character and so on.
So if I wanted to print last character off my string I will write X and end in a square record.
I will mention minus 1
we are getting L as our output since the last character it all forward very well.
X was an
while slicing.
You can also skip the arguments so if I write X and squared record I will write colon 5.
This means that I want all the characters from the start off my string line next 5.
If I add on this
you can see we are getting all the characters from the start of this string.
Similarly if I want all the characters from index 5 to the end of the string I would write X and record.
I would like five colon
can see all the characters from the fifth index is present in our output
now votive if I wanted done it.
Characters from my string I can do that by mentioning the third parameter which is the step that I make.
So earlier we were passing on the two parameters.
The starting point and then after the colon we were passing the end point we can also pass the Turk
argument after another colon symbol as a step argument.
So if I write X and then squared record I will write to colon then
this means I want all the characters from index to index 9 remember index expense is excluded.
But if I write to colon 10 colon 2 this means I want characters with a step as 2 so I will be getting
characters presented indexed to and since our step is still pretty radically jumped to index for
and then to index 6 and then to index it fight on this if I bring my X just to compare our output.
You can see our character at index 2 was space
character at index for was Q then character at index 6 was again in space and then character at index
it was you
similarly if you want to take this step size 2 3 4 or anything you can do that
1 think you may have noticed is that we are not defining variables while creating the variables as with
the other programming languages such as C askew L or C++ we don't have to define variable types and
by 10 by 10 will automatically assign the type depending on the value present in that variable so if
you are passing variable X as one by 10 will automatically assign X as an integer if you are passing
X as on a string it will automatically assign X as a string or if passing it as a decimal point it will
get a floating point type to know the type of variable we can use type function we just have to write
type and in parentheses write the variable name but here we will write X and we should get a string
as our output you can see the type of of what variable x is SBA similarly figured the type of forward
variable that is
variable 1
we are getting and since our variable 1 was containing 14 as a value
now if we assign variable to a value of five divided by two and we print this really way too you can
see that variable 2 is taking a value 2.5 if we want to get the tape of this variable 2 we will write
type and then record will write variable 2 we should be adding floating number as decimal is spelt in
our value.