Functions in Python are reusable code segments that organize code and reduce duplication, created using the 'def' keyword with parameters (variables that receive values) and return statements (which send results back to the caller). Functions can accept multiple parameters including default values, variable-length arguments using *args (positional) and **kwargs (keyword), and can return various data types including numbers, lists, tuples, or None. Local variables are defined inside functions and are only accessible within that function, while global variables are defined outside and accessible throughout the program. String operations include indexing (starting from 0), slicing (using start:end:step syntax), and various methods like upper(), lower(), split(), replace(), and endswith() for text manipulation.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Day 3 | Functions, Strings, and List ComprehensionsAdded:
Hey, hi everyone. This is Sai from Let's Up. Let's talk about uh how to get your certificate. It's very simple. You will see two links in the description. Number one is the place where you have to go and mark your attendance. Marking your attendance tells us like okay, you are a legit student who have attended this particular session. Link number two is your assignment. This is where your 90% of the priority is. You have to go and submit the assignment that is given to you in this particular uh session. Once you're done submitting that assignment, we will be evaluating that. We will send you an email. If you get the passing criteria, then only we will give you the certificate on a particular date that is mentioned again in the description.
Please check these three things. And this is the way how you will get a certificate from let's upgrade. Number one, mark your attendance. Number two, just go ahead and upload your assignment. Check for the email that you have received it. Yes or no? What is the feedback that you have received from your trainer? Number three, check on which date the certificates will be released. Thank you. Bye. All the best.
Happy learning.
Good evening everyone. Welcome to day three. In the previous session, we saw how to use list, how to use dictionary, how to use tpple. And today we are going to see how to use functions. So what is the meaning of function? So let's take one example. Consider a big organization that big organization divided into small small departments means okay okay departments and number of offices right to organize the work. So similarly here a big project big software big script divided into small small segments. So these segments are called as the functions. So we use functions to organize the code. Second, we use function to reduce the duplicacy in the code. So one function can be called multiple times with the different arguments. I hope you get a sense of functions. So we saw functions like range, input function, range function, uh list function. So these are the builtin functions. So we you we saw this thing but now let's see how to create our own function our own customize function. Okay, Python gives the power to create function and classes. In this session we will see how to create our own customize function.
Let's start. So we will see uh simple simple examples.
So how to create our own functions? So function definition start with the keyword defaf. Now what is a keyword?
Keyword means which are defined inside the python programming which are already defined which cannot be used as the variables. So def is a keyword which is already defined inside the python. So we cannot use this uh keyword as a as the variable. So first is first write deaf then take a meaningful name like uh I'm taking just a name great okay I can take anyone any name like function f1 so whenever we do coding for the client or industry we always give the meaningful name here we are learning so give any name then colon so this is the function name now it's a function body.
I'm just giving a statement.
Hello, welcome to Python.
Okay, so this is the function statement.
You can say function definition. This function definition will not work because we we haven't called it. So in order to make it working, we need to call it. So this is a calling of a function.
calling of function.
So when I call it, I'm getting its definition its uh output, it's uh print statement. Okay. So this is a very simple example of function.
So this one is calling a function or you can say invoking a function. Okay. So what is a whatever the name right here with parenthesis. Now let's see function with parameter. function with the parameters uh defaf function to so this is the parameter.
So this name is the parameter which takes the value right which takes the value then close it. So this is a variable. So name is the variable.
Okay. Print. I'm printing hello name.
Welcome to Python.
Python programming course. Okay. Python course. C O R S.
Done. Now I'm calling it with name. Uh let's take a name um Ivan.
Hello Ivan welcome to Python course. So I hope you get the sense how to use the parameter. So this is the parameter.
Okay name is the parameter and this Ian is the uh argument.
So when we when we use this argument that argument passes to this variable.
Okay. So this is a variable which uh parameter which takes this argument and this argument we use over here. So I hope you get a sense but consider if you forget to give this argument. Let's see I'm not giving this argument. So here we are getting error. Error is saying that missing one required positional argument. So this is a positional argument. So that's missing means we are not giving. So let's give it.
So now you can see well hello Ian welcome to Python course. So consider if I forget to use over here. So let's see what would be the error.
Function two takes zero position argument but one was given. Yes. So this function two takes zero position argument. No position argument but you are giving one. How to handle this thing? So if you get this kind of error check what is the issue. So now you you I hope you get in the sense what is the issue. Okay.
So function with parameters completed.
So I'm giving the multiple parameters so that you can understand uh def add a and b I'm creating a new variable inside it a and b plus c and printing c.
result is C lang C C okay done now I'm creating a two variables X and Y and calling a function with two these two variable X and Y when I call it I'm getting a result 30 so let's understand what's happening there so X sent to this one Y to this one.
Okay. 10 and 20 to this one. So this A became 10 B uh and B became 20C.
Okay. So I hope you get the sense. So in the memory if you give if I give a diagram of memory how it's working in the RAM. So it's a good to understand what's happening inside the memory. So x and y 10 here 20 here x pointing to this 10 y pointing to this 20 when x and y passing their values okay passing the original passing what is the meaning of passing here so x and y passing the original copy of 10 and 20 it means that inside the ram a became x b became y So now a and b also pointing to this 10 and 20 inside the RAM. Okay. So so that's why we are getting this uh result C.
So I hope you're getting the sense of this one.
Now next thing is returning the number.
Returning. What's the meaning of returning? Consider if you go to the any department or any office you give two three files okay means you are going in uh department to do your XY Z work and when you submit your files submit means you're submitting the arguments mean you're submitting the files they process the file and give you receipt return they're returning your receipt that's a result returning so that you can do whatever with the receipts maybe you want to submit to another department Okay, here we are not returning anything. We are just printing here.
Printing means they are showing the result. Consider that you are going to small office, submit the file, they're showing you okay, these are your result and go. But you ask give us a receipt. So that's a returning thing. So here we are not returning, we are just printing. So consider you know need this result C from this one for the further calculation. So I'm returning C.
It's returning C. And when I run it, when I run it here, I'm not getting anything. So here just 30 here. Okay, that's okay. But we need to print over here. Print it.
Now I'm getting the 30 result. So consider if I want to use this result for further calculation, how to use?
Consider D equal to whatever the result of X + 10.
So print D I'm getting 40. So whatever the its resultant that's a integer value that's a C and 10. So what is the meaning of we have to understand what is the meaning of return statement. Return statement means the resultant of this add function. What is the resultant of this add function? Consider by mistake if you give a here by mistake a here I'm getting 20 here because the result of this function is a whatever the calculation happen here doesn't matter for us what is the result here so this is the a is 10 10 + 10 20 okay so I hope you get a sense and the result statement after result statement so result is a final result of the function After that if you print hello hello world consider if you print it and when you run it I am not getting this statement hello world I'm not getting this statement you can see there's no hello world in the output but if I put this hello world here then run it and then run it here now I can see I'm getting the hello world statement and 20 result hello world statement this time is coming before the return statement but whatever outs after the return statement interpreter will not execute because the return is a final result of the function. If you want to close any function at a given point just return none. Give the return statement but do nothing return none.
Okay. So if you want to close any function at any moment just return none without if you you can return also a b c whatever but if you don't want to return anything just return none. Okay. So I hope you get a sense of return statement.
Now let's give the default arguments.
What's the meaning of default argument?
So I'm creating this program again.
defaf uh defaf function uh or function three a b okay c equal to a + b print I'm printing here or return you can also return here just I'm printing c uh result is c okay done uh fun three fun two fun three sorry 1 3 um 30 and 50 okay 30 and 50 I'm getting result 80 that's great but consider if I do not give okay if I call it multiple times uh 50 and 50 and 90 so I'm getting I'm calling the same function with different argument multiple times I hope you get a sense of calling multiple times so this uh this function calling with 30 and 50 argument a and b and this function this function three calling with 50 and 90.
Okay.
But here consider if I forget to give uh here if I forget to give one argument I'm getting error error missing one required argument B. Yes.
Simply we can see I missed the B. I'm giving the value of a but I'm not giving b. So if you see uh some some person are giving the value but some person are not giving the value. So in this situation you can give you can set the default value. Uh example is consider if I give default value 10. Run and run here. So if you are giving the uh value of b then b will take the your value uh 30 and 50 becomes 80. But consider if you are not giving any value here you are just giving value of a b is b uh is taking its default value that is the 10. I hope you're getting a sense. So default value if you are not providing it will take the default value. So the answer is 60.
Okay. So this is the default value.
Next is consider if you want to give multiple uh parameter multiple default parameters function for consider you want to give name age and marks right so I'm printing just a message name is Not here before this one. Just printing a heading kind of thing. Print name in the one sequence age and marks. Okay.
Print name age marks.
Function one name is consider ABC age 30 marks are 50 function one sorry function four this is not one four name is XY Z age is 35 and marks are uh 68 8.
Okay.
Fun 4 name is hit 45 age and not 45 32 age and marks are 80. Okay. So we are getting the good result. Okay. Calling a function multiple times. Consider this one missed the age and this one missed the number marks. Okay. So if I call it I can see error definitely. So I set default age here. Default age is uh like 25.
Okay. And also I set the default marks are the 40.
when I run it when I run it so I'm getting I'm getting result there's no mistake but logically there's a mistake why because for this one a name XY Z this is okay this is this is okay 30 is the age def that's great 50 is the marks that's right this 35 is the age yeah 35 is the age default you can understand that's a default default. So this is the default marks.
Okay, default marks.
But here what's happening? This is this is the this is I I know this is for the marks but it goes for age. Our intention to to our intention our what do I want? I want that this 80 should act as a age should go for age variable but instead of age instead of going here.
Okay. Instead of going here let me remove it.
Uh instead of going here, it went here.
Oh, sorry. It's it's a small mistake actually.
Uh this is for the marks. Instead of going here, it went here. This is right. No, sorry.
Instead of going to the marks, it go for the age. Go. It went for the age. So, this 80 became uh our intention to make this 80 as a marks. But 80 became age and default value of marks became uh the marks. Okay. So, how to deal this kind of situation? how to deal with it. So we need to take care. We have to adjust it.
We have to adjust how.
So if our intention this 35 for the age that's great but this one this is for the marks. So we need to tell explicitly this is for the marks. So when we tell explicitly this is for the marks. You can see now this time 80 80 is a max 25 is a default age. So this time this function this one is picking marks as 80 and and age 25 that's a default. Okay, I hope you get the sense. So this one is the keyword positional keyword. Okay, keyword argument.
Next is the default argument. Uh variable length argument.
Variable length argument. Yes, variable length. So consider defaf function uh five. I know a I don't know b here.
So print a and b.
Print b is and b is I'm calling function uh two three times.
First give 10 and 20. Okay. Function five.
Give four only.
Function five 90 3 4 5 6 9 0.
Okay. We are getting a result for this 1020. I'm getting this A 10 B 10. for this uh sorry but for this uh statement uh uh for this statement I'm getting issue what's missing one required we know very well this issue so let's put a default value zero now this time we are getting a 10 b 20 a4 b is zero that's great but for this one I'm getting issue that's our function can take one or two position argument but seven one given means our function is capable of handling one or maximum two arguments but you are sending seven how how we can handle it in order to deal this kind of situation we use variable length argument so put star over here when we put star over here star asterisk sign here it's not a pointer it's not a C language pointer it means that it is it is like a tpple right so 10 20 that's great 10 10 is a single element went to A.
Okay, A. But 20 is kind of tuple with single element. This is a f first call.
First call. This one is a second call.
Second call. A4 B is empty. This is empty tpple because we are not sending any any argument here. So B became the empty tuple. So in the third call this one this is third one third call 90 90 went to 90 goes to a the rest of the element are the values of a tuple.
Okay in this way we can create the tpple with the uh variable length argument with the help of tpple. So I hope you're getting the sense if you want to send multiple item zero zero uh zero arguments. So you can use variable length argument. So consider if you want to send the keyword quags. So we in the general term we use this uh we use the terms arguments arguments multiple arguments args but just a general we just a convention.
Okay if you read a book you will see a args. This is a convention we use when whenever we want to use variable length argument we use this convention arcs complete. Okay. Next is a quarks.
Consider if you want to send the keyword dictionary kind of elements. Okay.
Dictionary kind of element like function. We have already sent dictionary kind of this one. This is kind of dictionary kind of arguments. So if you want to send all the argument as dictionary kind of arguments.
Let's uh make it uh def um a um all the arguments. So we use quarks.
This is kind of dictionary kind of argument. This one. Okay. Here we are taking a single as a default argument.
But if you want to take as a dictionary a w kw a rgs quarks and uh function 7 like uh name equal to Intel processor equal to um processor equal to i7 RAM equal to 16 GB.
So now you can see we are getting a dictionary name Intel processor i7 RAM.
So we are getting a dictionary.
Okay. So if you want to send these element in one argument means variable length keyworded argument.
In previous previous go in the previous one we have two argument which was fixed but we cannot give three four five. Here you can give multiple arguments and we have one argument which can take it.
Okay. So variable length argument means if you have multiple arguments uh multiple positional argument here if you have multiple positional argument then quarks will handle this double star will handle it. Now we need to see the order first. Now okay if if you have all the argument in one. So first positional argument then default argument then variable length argument like args then keyworded argument.
Now your program can take any kind of argument but one must be given.
Okay.
One must be given. 1 8 10 20 uh 60 50 consider if I give just this thing. So I'm getting it. So our function is not giving error. Okay. I I I I need just first argument and um uh consider C equal to U 78ide by A and return it.
return the C.
So I am all I need only A but I want to need A and B but I want that our function can tolerate multiple type of argument. It shouldn't fail okay shouldn't give error it can take it can handle multiple time error multiple multiple argument uh for to handle the error as well as to uh for future use.
Okay, consider if somebody give the value like name equal to 10 just so this will handle it. Quarks will handle it. We cannot change the position of it. Consider if you try to change the order you will get the error.
Okay, invalid syntax. So this kind of syntax cannot be used. First po first positional argument, second default argument, third is a variable length argument. Fourth is a quarks keyworded argument. I hope you're getting the sense how to deal with it. Okay.
So I hope everybody is getting the sense from uh ERGS.
Remove this comma. This is not good.
Okay. So I hope getting a sense. So next question. Next thing is the local variable and the global variable. What is the meaning of local variable and global variable? Local variable which are inside which are defined inside the function. So this is the last topic from the function but which is very important uh function a and b c= to a + b print and x = 10 y = 10. y = 20 and function 1 = x + y. Okay. So what are the local variable? What are the global variable?
So local variable means which are defined which are defined inside the function which are defined inside the function.
So a b which are defined inside the function and c. So a b and c these are the local variable global variable which are defined outside the function.
So x and y which are defined outside the function. These are the global variable.
Okay. So I hope you get the sense of local variable and global variable.
So let's do one or two experiments so that you can get the clear idea. Okay.
If I create here A and B.
Okay. So now you're saying now we can say A and B are the local as well and it is a global as well. Okay.
So what is the meaning of this one? So ARB local and as well as global how? So we need to understand. So this is a global and this is local. Okay. So in the RAM you can understand by using this example 10 and 10 and 20 a and b. So a g I can write small g which is pointing to 10 and 20 when I pass this to this one and this to this one. So another variable a small l b small l means b local. So another variable A local A global B local B global. So two variables are created. So you want to understand if you want to understand consider let's change this time we change a A here and print a A B. Okay. Print A.
Print outside A.
Now inside 30 and outside uh just see what's happening here.
Hm outside remain a outside it was a remain a as well and inside it we passes the value 10 but it became 30 okay because this is the local variable getting sense so uh inside the function I'm reassigning a again okay reassigning a again it's not the global one it's the local one whenever we define here that's a local variable.
So I hope you get the sense of global and local.
Okay. So consider if I want to use C if I I'm not sending any variable over here.
Just imagine I'm not sending any variable here. Uh A and B print C.
A okay. Okay. I'm not passing any value.
So what's happening here? I'm not passing this time. But what from where we are getting this A and B? Uh actually uh here we are not changing the A and B.
We are just using it for reading purpose. So for reading purpose when we use it function checks the local scope.
Function first check the local scope. Do we have A and B? No. Then function check the global scope. Do we have A and B? Yes, we have A and B in the global scope. Use it to calculate this C.
Okay, calculate the C. But consider if you go just one step from consider if you make it A here in this situation we will see error.
This type of things is not allowed because because whenever because because if you use this statement a it's check do we have a in the in the local scope.
Uh let's see the error here. local variable a referenced before the assignment. Okay.
Uh yes. So when we create a it means that this statement tell this is the local variable because a we are defining inside the function it become local. And uh next thing is say a here. A also here. Oh my god. A is also here.
Now a is a local but you are using a here which is not defined in the local scope. That's why error occur.
We are saying a is a local but a is not present in the local. So if I create a before that a equal to 10 it works. But consider if I remove it, I'm getting error. Okay.
But what you can say a is here. But that's in the global scope. When we create this statement, it tells a is local. It say a is local.
But what is this? What is about this a means you are saying local but you still referring it. So consider if I create here 10 just imagine it works because 10 + uh b that's okay. We are creating a local variable a 10 + b that's okay. But if you are creating a local variable with the help of a not it's acceptable.
So in this situation if you want that no I want to use this a any cost because I want to change it inside the function.
So in this suggestion we have to tell explicitly use global A means you are creating a here which is a local but this A is the global fetch the A from the global scope if you are creating in the local. So in this situation it works and uh inside outside it became 30 30 because now a is the global we are explicitly saying you are changing it but take it from the global scope. We are changing the global inside the local scope.
So if you want to change the global value inside the local scope, we have to use the lo global statement. This is applicable for only for the mute immutable objects.
Yes, this is applicable only for the immutable mutable.
Okay, I hope you get the sense, right? So consider if you are using any list. Yeah. Yesterday we saw how to use list. Uh if I create a list over here, let's suppose a list here list contain um 10 and 20.
But here if I use it because uh local scope uh because here I'm not reassigning it. I'm changing the original one. I'm adding 90 and printing.
I'm not resigning it. I'm changing the original one.
So outside list one. So inside outside both are same.
You can understand we are not I'm I'm not reassigning it. In the previous example I was reassigning the A with the help of assignment operator. But this time I'm not resigning. I'm just changing it.
Okay? I'm just uh appending uh modifying it. Appending it so it will work. So I hope you get a sense of uh local scope, global scope. I hope you're getting a sense from the function.
Okay.
Now let's do some more example by using the function. So we have completed local and global scope. Let's do some more examples. Consider with the return statement. Consider I want to check whether the number is uh uh even whether I want to find even and odd. So I'm creating a function check even uh check uh e and o even odd. Number is num one.
So I'm going to use a multiple return statement like if num one divide by modus 2 equal to equal to zero in that case I can return it even else means I'm using two statement return odd uh check eo with number 10 even okay print it because we are not printing we are not getting good way yes now we are getting 10 11 even a note multiple statement okay multiple return statement and uh if uh if you want to send a complete list yet you can also uh give the complete list def function M return statement can return a list also function one I'm getting a list so print over here so you want to know the type of function so that is a list type because it is returning the list so it's a return type is list okay if you want to return two statements two numbers. So defaf function two consider somebody say can a function return two statements two numbers. So yes return a return uh return a equal to 10 b equal to 20 and return a and return a b print 2.
Oh, invalid syntax. Okay. Comma. You we have to use a comma.
So when we use comma, so it take by default tuple. So we know very well whenever we don't define so it uh interpreter automatically take it take it as a tuple. So this is the tpple.
Okay. You can return multiple argument but uh return but when we print it, it always take one and that is a tpple. I hope you get the sense. So similar way we can return the dictionary, we can return nothing. If okay consider one function return nothing it's return nothing so in that situation it return none by default none okay sometime interview ask this question if function doesn't contain return statement what it returns it returns none so if I return just return statement none okay so return uh with no statement return none return without statement without uh this uh keyword return none Okay, every time return none. If you define it, return statement none. If uh if you not define then also none.
So I hope you uh get the sense of uh of the functions. So do one exercise uh function that calculate the area of rectangle.
Okay, area of rectangle. So create create a function which calculate the area of rectangle, area of uh area of triangle different function created.
Now we will see the string. So we have already seen the strings. Now we will see some operation. We will we will apply some operation on this string.
First of all let's start with this definition. What is the string? So string str1. Okay, little bit zoom. str1 equal to if I uh what is a string here?
Actually uh uh zero or more characters uh inside the quotes that is string.
Okay, I can use single quote. I can use double quote both are same in the Python. Okay, that is a string Python. So this is a this is the string.
Okay, if I print it, Python. Okay, so slicing indexing. So we know very well indexing. So let's do some recap of indexing. I'm creating a diagram so that you can clearly understand what is the meaning of indexing here. Okay. Uh I'm taking a different string so that we can understand completely. str2 equal to hello jarvis.
Okay. So now I'm saving it in the array so that you can clearly understand what is the meaning of this one. Okay. H E L L O space jar Jarvis. Okay. So this is at number zero 1 2 3 4 5 6 7 8 9 10 and 11. So if I do minus indexing there is no min -0 okay -1 - 2 -3 -4 -5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 okay so this is the negative indexing if I want to know space so I can use str2 I can use minus 7 or - 5 oh sorry + 5 both are space okay if I want to access what is at number four simple okay what is at number 12 error okay we know very well there's nothing at the number 12 the last number is 11 because we started from the zero so if you want to know the length of a string so length of a string is str2 that that is the length of a string 12 character 1 to 12 but indexing always start from zero negative indexing always start from one okay so this is the indexing if you want to know the last character so you can use minus1 okay s character minus one getting sense so if you want to do uh uh slicing slicing means getting the substring okay getting the substring from the string. Okay. So the syntax is uh string. We can say string string whatever string. This is a start number.
Start number and and you can say step.
Okay. Step. So I'm just taking the str2.
uh start number is uh two and go up to six. The last number is exclusive.
Please uh remind it please pay attention. Last number is exclusive. So start from two. You can see the diagram.
What is the second number? Second number is the L then L then O then space. So last number six is exclusive. So 2 3 4 5. So this this is the number. Okay.
So next is consider I want to know from um 1 to 20 there's no issue with when we do slicing. Okay. There's no issue with the range here.
Issue only with the indexing. In the slicing there is no error 1 to 20. So whatever the last number that's uh 11.
So I'm getting hello jar. So if I start from zero that's great. So consider if I want to print complete string complete so I can use start from zero right start from zero and whatever the last number start from zero whatever the last number. So string always uh if you let's do some steps uh let's uh 0 to 8 take two two steps.
Hello. Take two two steps. Okay. So, start from zero.
Take two two steps. Let me create a diagram. Start from zero.
This one.
This one.
Okay. This one.
And you know very well last number is exclusive. So up to only this.
Okay. Last number is exclusive. So eight will not be added included. Okay. So I hope you get a sense of next.
So if we I want if we want to do indexing from actually indexing always start uh sorry slicing always start from left to right. Left to right. Consider if you give number like uh 8 to 0 what we will get? So left to right. So this is not uh applicable here because by default we go in this direction left to right direction. So we are saying go to 8 to zero. No this is the uh this is become right to left. No by default we go left to right.
But consider if you want to go in the reverse direction so you have to use minus ones. Take the steps but in the reverse direction. Take one one step but in the reverse direction. So you can see this is the 8 to 0 in the reverse direction. You can also take two two steps.
Okay. A to 0 take two steps in the reverse direction minus direction. Okay.
You can also use negative and positive indexing.
So consider I'm I'm using 1 to minus one 1 to minus one both are applicable left to right okay consider you want to get first four numbers first four numbers so I don't know starting I know this is starting from zero but I can leave it and four so these are first four character hell okay okay okay first five character Hello. Okay. Consider I want to know uh after fourth character. After four what is the string after four character?
Leave first four then. So it's O Jarvis.
Okay. Leave first four then all first four vectors. I want to know first four. First five use this one. Okay.
Leave first first five like this space Jarvis.
Okay, getting sense.
Consider uh I want to start uh if I want this what is this one? This one is the hello Jarvis. Whatever the starting whatever the ending uh print complete string. Now the question is if I want if I do whatever the starting whatever the ending go in the reverse direction. So you can see this is a reverse of the string. If somebody asks you question how to print how to create a new version of a string means reverse version of the string. So this is a reverse version of a string civ only. Okay. So this is a reverse version of the string. Whatever the starting whatever the ending go in the reverse direction. Okay. If you create two here, this is the reverse but take two two steps. Okay, I hope you get the sense of indexing and slicing.
Now, how to use the for loop over this one? So, we already did for loop in the first uh for loop section, but here I'm also repeating it. I'm also doing with string.
So in this way we can do. So if I want to print in the horizontal fashion, I can see we can see hello Charvis and there's no space. So hello Charvis in the horizontal fashion.
Okay.
If I give space here, hello Char space.
Consider I want to know index also index of each string. So I can use enumerate.
So for when we talk about enomerate we get two things for each in anomate str2 print I add the zero number index. So indexing.
So you can see zero number index we have uh min -1 + 1 e uh sorry 0 + 1 2 3 4 5 6 okay so fifth number we have space. So same thing what whatever I designed here in the diagram. So we are getting here.
Okay.
So consider if you want to know whether uh the given character exist given substring exist or not. So we can use in operator.
Okay. In uh consider I want to know H in str1.
False. H is not in the okay str. Sorry.
Yes. H is in the str2. If you want to know a in this yes true if I want to know x no x is not in the str2. If I want to know hello, yes, true. Hello is there. But hello, no. Okay. If I want to know substring present or not, we can use. So we have in or not in not in true. Hello is not in. Okay. Do we have Z? No. Z not in. Yes, true.
Okay. So I hope you're getting the sense of inoperator.
Then let's use let's uh apply some methods of string.
So if you want to know what are the different methods are available what are the different method available in the string. So we can use diir or trir over the str2 or str just simple str also. So these all are the method available in the string.
Okay, these are the methods available.
Okay, so let's apply some some of them like first case method case which deals with the case. So consider if you have any numbers like your Aadhaar card number or sorry your pen card number like uh A B 2 3 H I Y U some numbers. Okay, I want to copy paste somewhere. So I need but I need the uppercase version of it. So I can use upper.
Okay. I am getting a new string which is a uppercase version of the str2.
Original string remains same. Okay. So pi uh string method doesn't change the string. Please remember it. String method doesn't change the string. It creates the new version of existing string.
Okay. So string is immutable.
Yes, string is immutable.
So upper is this one. This is this this presents the upper version. What's happening here?
Uppercase version. So if you have upper like can if I take a string this is in the str okay okay I'm creating a new string like str3 what we think we became okay and I'm going to make it upper lower case all so you can use lower so in this way you can create the lower case if you want to create upper case we have already did this thing okay if you want to create title case.
Okay, every letter every letter of the sentence uh word is is in the upper case. This is title case. So we have other one also capitalize capitalize capitalize.
Uh oh capitalize capital let me check what is the spelling over here. Capital okay sorry capital it's English yeah so first letter only capital okay first letter so you can see we have done here decimal starts with swap cases lower to upper upper to lower case title case we did okay upper case we completed okay so these are all our method you can use. Oh, consider if you want to know how to use particular method, how to use particular method.
So, you can use help and write here str uh upper.
Uh so you can see this is syntax. Use upper okay no argument inside it. Return a copy of a substring converted to uppercase. Return a copy. Always remember it's a copy. It's not original string. Okay.
Now replace method replace. If I want to know how to use replace use replace so this is this is the code. So old string new string and by default count is minus one. Okay by default value is minus one.
Minus one means all occurrences like we have str what is str 3.
I want to change we str3 replace uh what is the old string that is the we and new string is I so you can see I'm getting a new version of this string what I think I became okay this is a new version of a string okay if I want to set occurrences like uh just rem just uh uh change the first occurrence only what I think we became okay I hope you're getting the sense of replace next is the split this very very important split method split so what is the meaning of split so you can see split here r split and split both brothers are here uh yes so r split is there and split is there split so very very important method split means split the spring based upon on some kind of delimiter. So example consider I have um I have uh date uh date uh 2025 uh let's take a date uh 01 and uh 10 uh 15. Okay, this is a date. I want to split it. Split means I need uh this one is separated. This one separated. This one separated. So what I'm going to do str1.split split delimiter is hyphen.
So, so this method returns a list of list of splitted string.
So, hyphen may based upon hyphen splitted. This is the first one, second one and third one splitted. Okay, based upon hyphen splitted. If I provide hash now you can see hash is not present in the string. So that's why we are getting only a single element in the list because hash is not present. Okay. If I do not present if I do not give anything by default it take the space as a string. So our str want to know how many words are present.
So how to do split based upon spaces. So we have a list over here which contains five words. So how to calculate? So this is a list. I can apply length over it.
Length function. Length is a function built-in function in Python which finds the length of a list or or string. Okay.
So whatever the data structure. So consider I have list or string whatever I want to know I want to find of the I want to find the length of this list one three similarly this is a list and if I apply length it returns five words are present in str3 so this is very shortcut method if you want to know how many words are present consider if you want to know how many letters are present in this string So you want to know letters. So you can directly apply length over it.
23 characters are present. Five words are present. Okay.
So this is a split method. So I hope you're getting the sense. So consider if you want to split if you want to split but if you want to split only the two correct two time uh oh two time so you can see what we think we become so this one so two two times split happen okay not all time so consider if I have IP address kind 192.168.10.11.
This is IP address. IP.Split dot two time. So I'm getting two time.
So first split is the first split.
Okay. If I draw it so that you can understand. Let me draw it.
So this is the first one. Second split and okay. Two splits are there. First, second. So this is a third piece.
Okay, I hope you're getting getting sense.
But if I do not define so it will take all splits happen.
Okay, I hope you get the sense of this IP address.
So try try yourselves try these methods with yourself. Is numeric is alpha is digit. So consider if you want to know particular element is is digit or not.
Okay. U like u um str1 equal to 90. This is is digit or not is digit.
Yes. It's a digit or digit. If I say a no it's not digit. So let's create a small program. Consider uh um marks equal to intput.
Enter the marks and uh print marks. Okay. Whatever.
So enter the marks. If somebody enter 19 that's okay. If somebody enters a19 error. Okay. Because this number cannot be convertible with the integer. Okay.
That's a problem. So what to do in this case? Take whatever we have, whatever user enters.
If marks is digit if convertible, then converts to integer form then print as give the message.
Please print please provide please provide integer.
So, if I give 90 uh-oh 90 if I give a 90 please provide integer this time I'm not getting the error I'm getting the relevant correct relevant message to correct user okay so this in this way we can use it so it's not uh easy to complete all the methods here.
So you can try okay like Z field. So you want to know how to use Z field. So I told you how to use it help str Z fill Z fill or Z fill. So it paired the numerical string zero on the left hand side and filled fill the field given width. So this one so width. So you have to give number uh numerical string. So fill the field with given width. So how to use um like uh consider you have a binary number uh number n number um okay this is a number consider this is a count number okay okay and I want to fill it Z fill with 10. So it automatically make it 10 number long and uh remaining things. So four are there total make 10 width and now six zeros has been added in the beginning.
So in this way Z field works. So so it is an exercise for you to apply maximum other methods.
Okay other methods of the strings.
Okay. So small exercise. So I'm giving you a small exercise. Uh create a program to take the list of uh student marks, convert them to grade and display the result. Okay, we did this thing in the if l if section. But now try to create the program with the help of string.
Take the marks and apply this integer.
If somebody give the wrong marks then correct it and then convert to grade with the help of if else structure and then display the result. Okay, I hope you are getting the sense how to use string. Let me give you one uh real kind of application real application of it.
How to use strings and string with list.
Okay. Uh consider I want to know how many u Python files are present in the particular folder. So this is particular folder.
I want to know how many Python files are there.
Okay. Let me create one simple example.
How many Python files? So I want to know of all the files. So I have OS module.
OS.list will give me the list of list of all the files. So this is a list of all the files. Okay. Now I have a list. So I can use for loop over it for each in print each.
So now I can see I'm getting string.
This is string. This is string. This is string.
So I have a a lot of strings here. Okay.
So some folders are present, some uh some folders are present, some files are present. I want to know Python files. So what is the u you can say what is the um uh what is the python files? So which ends with py.
Okay. How to identify python files which ends with py ends with py. So what to do? We have a function called ends with let's see where is the end ends with uh uh ends with center count in code ends with okay ends with. So let's apply ends with each dot its check ends with what is py.
So last actor must be ends with py. So waiting saying false false false.
Okay. True true true false false. if print each.
So these are the Python files. So this in this way we can use a real life examples. Okay. Find all the Python files from the particular folder for find all the P python file from this folder. I have created lot of application by using string list. So let me show you those commands.
Uh consider G drive. So this is a Windows command prompt. Okay, Windows command prompt. And if I go to the similar folder, okay, in this command for prompt folder, if I want to know all the files, so uh the uh command prompt offer the diir command, right? So but I love uh Linux operating system when it comes to the programming. So there's a command called ls. LS command doesn't exist in the uh windows system. LS or grab command. But I created my commands using Python and converted to.exe file and then I can use it here flawlessly. Okay. LSTR command is not a part of Windows system but I created for me. I created myself.
I created myself. Okay. And put in a command in my folders. Now it's working well. So anywhere I can use this command. Okay. Lsltr ls command. Okay.
So in this way uh I have created this program in the python for my convenience. So I have created a lot of command lot of things. Uh there's a real life example consider finder if I want to know if I find find if I want to find any file in my computer system it will do in just in one go. So this regular expression whatever the file ends with the PDF. So this regular expression oh in the note here uh finder is not okay.
So it will work from here.
So anywhere finder C H something something called PDF and ends with PDF.
So this is regular expression. So now I can see it is working in the background.
Very soon it will give a list of all file which contains C in the middle and ends with PDF file.
Now I can see I'm getting these files.
So C in the middle or somewhere ends with this one. Consider I want to open this one 11th number file. So I can use F 11.
Uh it's stop opening second time but it's opening in the background. So it's presenting of D4 open folder F4. So okay that's great. So now I can see I have opened it. Okay this file has been opened.
So I have created this program in Python. Okay. So note uh I have created uh 8 years ago these programs in Python.
Okay. So I hope you get you enjoyed the session. Okay. And you saw the real life application by using list uh dictionary uh tuple strings for loop while loop.
Okay. Bye-bye everyone. Bye-bye.
Heat.
Heat.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 views•2026-06-01











