‫Now let's move on to strings in Python.
‫So how can we pass a string in a Python, if I just write st python tutorial and execute it
‫I'm getting an error because we need to pass strings in double quotes or single quotes.
‫So we can write in double quotes st python tutorials.
‫Here we use double quotation marks.
‫You can also use single quotes to pass string.
‫But sometime your string may contain single codes in between.
‫So therefore, it is recommended to use double quotes while passing strings.
‫Just like numbers we can also assign
‫This string to some variable so we can write str one, here str one is our variable and we are assigning
‫value st Python tutorial.
‫Now, my str one variable contains this value.
‫Now, if we want to display the value of our str, one variable, I will just write str one and then
‫execute this
‫Command
‫You can see we are getting our output.
‫And you can also notice that st python tutorial is coming in quotation marks.
‫There is also a separate print function in python to print the output.
‫To print output will write print and in brackets will write str1.
‫You can now notice that there is no quotation marks.
‫And now the values are printed and they are not a part of output.
‫You can see when we just executed str 1, we were getting the content of this variable in
‫the form of output.
‫You can see this out written here.
‫But when we are printing it, python is actually printing it.
‫And this is not a part of out
‫Python even provides formatting options with this print function.
‫So suppose I wanted to use some variable inside my print
‫Command, I can do that by using print formatting.
‫For example, in our previous example, in the phrase st python tutorial, I wanted to pass
‫variable X, which will take python or R depending the value.
‫So if my x is python, I wanted to print st python tutorial, if my x is R, I wanted to print
‫Str tutorial or if X is sql, I wanted to print st sql tutorial.
‫So let's create this variable X and assign it value R
‫Notice that we are using double codes to assign value to this variable.
‫We will first write print
‫And in brackets we will put quotation marks and will pass the array string, which is S-T.
‫And then since I want my second word to be this variable X, I will give curly brackets.
‫And my third word is a constant, which is tutorial
‫So I will write tutorial.
‫Now, I want to tell Python what goes inside this curly bracket.
‫So after this print command, 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 error because dot format should be inside the print
‫Command.
‫So we just copy paste it
‫Yes.
‫And now if we execute it, you can see we are getting
‫Str, tutorial.
‫If I change my value of X to SQL and then again run this print command
‫You can see now since our X is SQL.
‫The output is statement is st sql tutorial.
‫So what's happening here is, the curly bracket in print command tells
‫Python to take the value of variable that is mentioned in the dot format
‫Parentheses.
‫If I want, I can also pass two or more than two variables.
‫The syntax will remain the same in that case.
‫And you have to mention the variable names in dot format in the order you want them to be filled
‫in the curly bracket.
‫So if we just write print.
‫And in bracket will write S.T..
‫Then curly brackets, here our variable X will come.
‫Then after this tutorial
‫and then another curly bracket.
‫And in dot, we can write X and the variable we created earlier, that was var one if we execute this.
‫You can see our var one variable was containing 14.
‫So our printed string is st sql tutorial 14.
‫Now let's discuss some important functions related to strings.
‫The first function.
‫Is length function.
‫It helps us to find the length of our string.
‫The syntax of length function is l e n.
‫Now, if we want to check the length of our str 1 variable we can just mention str one inside parenthesis
‫After length
‫You can see the length of our variable str one.
‫Is 18
‫Also note that is space is also a character.
‫So this length 18.
‫Is count of all the characters, including spaces and other punctuation marks as well.
‫Another string function, which we frequently used, is replace function.
‫You must have used replace function in word or notepad, where you search the word and then replace
‫that word with a new word, replace function in python works
‫Exactly the same.
‫You have to give two words.
‫.First, the word you want to search.
‫And second, the word you want to replace it with.
‫So let's again print our str 1 variable
‫right now.
‫My str one contains st python tutorial.
‫If I want to replace this word python with sql, I will write str one dot replace and in
‫Brackets first 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 contained python in lowercase.
‫I want to replace this variable with sql.
‫So after a comma, I will write a sql.
‫You can see that the word by python.
‫Has now been replaced with sql.
‫I can also save this value in another variable.
‫So let's save this value in another variable called x.
‫I will copy paste sole function.
‫If I print x
‫I'll get this, value st sql tutorial.
‫Now let's move on to string indexes and a string slicing.
‫You can see string is a collection of several individual characters.
‫For example, in our variable X, s is a single character, t is a single character.
‫And our whole string is a collection of such characters.
‫These characters are present at a specified position in our string.
‫So, for example, s is present at the first position, t is present at the second position.
‫And similarly, we have a space
‫At the third position.
‫We can extract this individual characters from a variable by specifying their positions.
‫This concept of extracting characters by their position is known as string indexing or string slicing.
‫One thing to note here is that in python, indexing starts with zero.
‫So our character s is present at zero th Index t is present at first index and space is present at
‫two index.
‫So if I want to extract the character which is present at the first index, I will just write our
‫Variable name, which is X and then the square bracket, I will mention the index, which is one.
‫If we execute this, we will get t
‫As our output since t was present at the first Index.
‫Position is different from index, the position of t is two.
‫But since the index starts from zero, our s was present at 0th index and our t was present at
‫First index.
‫I can also mention multiple index.
‫So if I want all characters which are present from index two to index five.
‫I will write X and in squared bracket
‫I will write two.
‫Then I will provide a colon symbol and then I will write five.
‫This means that I want all characters which are present between index two and five.
‫Notice that our first character is space, our second character is s, and our third character is
‫Q.
‫We are getting all the characters that are present at index two index three, and index four.
‫We are not getting our indexed five variable because the second argument is always excluded while slicing
‫the string using indexes.
‫So two colon five means character present at index two, three and four.
‫Similarly, if we write bracket four Colon six, it will return the characters which are present at
‫index four and five.
‫Index six will be excluded from our selection. indexes, also supports negative argument.
‫Negative stands for counting from the backside of this string.
‫So minus one will be the last character of string, minus two will be the second last character and
‫so on.
‫So if I wanted to print last character of my string, I will write X and in square record I will
‫mention minus one.
‫We are getting l as our output since the last character or our variable X was l
‫While slicing you can also skip the arguments.
‫So if I write X.
‫And in square bracket, I will write colon five.
‫This means that I want all the characters from the start of my string till the index five.
‫If I run 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 five.
‫To the end of the string, I will write X and in bracket, I will write five colon.
‫You can see all the characters from the fifth index is present in our output.
‫Now, what if, if I wanted alternate characters from my string.
‫I can do that by mentioning the third parameter, which is the step parameter.
‫So earlier we were passing only two parameters.
‫The starting point.
‫And then after the colon, we were passing the end point.
‫We can also pass the third argument after another colon symbol as a step argument
‫So if I write X and in squared bracket, I will write two colon ten.
‫This means I want all the characters from index to index nine, remember index 10 is excluded
‫But if I write 2 colon 10 colon 2, this means I want characters with step as two so I will be getting
‫characters present at index 2.
‫And since our step is two it will directly jump to index four.
‫And then to index six and then to index eight.
‫If I run this
‫If I print my x just to compare our output, you can see.
‫Our character at index two was space.
‫Character at Index Four was q.
‫Then character at Index six was again space and then character at Index
‫8 was u.
‫Similarly, if you want to change this step size to three, four or anything, you can do that.
‫One thing you may have noticed is that we are not defining variables while creating the variables.
‫As with other programming languages such as the C, SQL or C++, we don't have to define variable
‫types in Python.
‫Python will automatically assign that type depending on the value present in that variable.
‫So if you are passing variable X as one python will automatically assign X as an integer.
‫If you are passing X as an string, it will automatically assign X as an string or if you are 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 parenthesis,
‫Write
‫The variable name.
‫here we will write X and we should get a string as our output.
‫You can see.
‫The type of our variable X is str.
‫Similarly.
‫If we get the type of our variable that is.
‫Variable one.
‫We are getting.
‫And since our variable one was containing 14 as a value.
‫Now, if we assign variable 2 a value of five divided by two and we print this variable 2,
‫too, you can see that variable 2, is taking a value 2.5.
‫If we want to get the type of this variable to, we will write type and in bracket will write.
‫Variable 2 we should be getting floating number as decimal is present in our value.