[no audio]
Friends, here we are going to
do one simple practice with your for loop.
I mean, I'm going to write a simple Python Script. The requirement
is, read a string and print characters and their index values.
I mean, suppose if you run your Python Script, it has to ask
'Enter your input'. Then enter some input.
Let's say I am going to enter input as 'python'. Then it has
to give output as, in this format.
'p-y-t-h-o-n'. At the same time
it has to print the index value of your letters
or characters in this way.
[no audio]
Right. So, guys before going to watch the next part in this video
first just pause this video and try to write your script
to read some string and to display your output in this way.
Right. Just, you just give your try, then come back and watch the
remaining part, right?
Okay, but as of now, I am going to write, you just pause it and try.
script from your side.
Okay. Let me save the code as, I mean script name as
'read_string',
[no audio]
then, '_print_with_index_values',
[no audio]
'.py', right. See, you know, if you want to read a string
what you have to do?
Let me take, 'usr_str'
I am taking, equals to, I need to read a string. You know,
if you want to read a string you have to use 'input' syntax,
right? So, what I am doing is "Enter your string :".
[no audio]
Okay. Assume that user is going to enter anything,
any string, right.
Now, what I am doing is, see here
'for each_char in usr_str'
let me print first all characters one by one.
Right. Now, let me my command line because I need to provide
input for script, so I am going with this.
[no audio]
Now, let me open your script.
'read_string_print_with_index_values',
this is your script.
Now, I am going to run that script, right. See that. 'Enter your string :'.
Suppose I am entering 'python'. You can enter anything.
See, what is happening.
You are entering 'python'.
[no audio]
What I am printing?
[no audio]
Oh, I am printing 'usr_str', not 'each_char'.
I want to print, then only I can get one by one character,
right? Okay, let me rerun.
So before going to rerun, so guys, what is the mistake
I did means, in each and every repetition with your loop
I am printing complete string, but I should not print complete
string, I want to print character, whatever the character
you are getting into this from your entire string one by
one that character
I want to print. That's why just now I modified your script.
Now see I am printing each character, right. Now
let me run your script once. I'm entering suppose 'python'.
Yes, you are getting 'p-y-t-h-o-n' separately
in each and every line. But while printing your character, right,
what is our required output? 'p', some '--', arrow mark,
then some integer number.
So before that what I am doing is,
[no audio]
see I am taking 'f' strings,
your variable value, then '--' suppose,
in this way
I am trying. Just see the output first.
[no audio]
Now I'm running this.
I'm providing string as 'python'.
Yes, you are getting 'p-->' symbol, but after
that what you have to print? Here, you have to print 0, 1, 2,
3, 4, 5, likewise based on the number of characters you have.
See that simply what I am doing.
[no audio]
Where is your script?
Yeah. See, what I am doing is I am taking, before your loop
I am taking some index value, or simply 'ix', or simply 'index',
let me take 'index=0'. Initially
I'm taking index value as 0. While printing first variable
value I will print this index as it is. See that.
[no audio]
Right. Now first
let me run it so that you will get idea.
[no audio]
This is your script.
What I am doing is I am printing your character and then
index. What is the index, 0.
[no audio]
See guys, suppose if we enter 'python',
I mean while running your script if you enter any string,
[no audio]
suppose I am entering 'python',
so then if you observe your script, what will happen? This
'p-y-t-h-o-n' will go and replace in this variable in your loop.
Now, this is 'p-y-t-h-o-n'
[no audio]
Now your loop is going to take one by one character, and it
is going to store in this variable and that variable we are
printing. At the same time
we are printing index also, but index is a constant value.
That's why if I run our script, see the result
what it is giving. For all characters, for all characters
it is giving 0, but 0 is not the correct index except your 'p'.
See, what I want to do is while printing 'p' I want to print
initial value 0. So your loop is going to take second time second
character from your given string.
So before going to second, before going to get second character
what I am doing is after your 'print' statement inside of your
code I am increasing index value by one. 'index = index + 1'.
[no audio]
Now let me explain here itself. Suppose assume
that you are entering string as 'python', then definitely your
Python will replace in this way internally.
Now see I am taking first 'p',
I mean your Python and it is storing into this variable. Then
after that whatever the lines you have those lines will repeat once.
Right. So, while printing this index value initially it is 0, that's
why 0 will print. Then your Python will execute next line.
So, what you are doing? You are increasing your index value
0 + 1 = 1. Now in your hand index value is 1. Then your Python
will go and take next character from your given string, 'y'.
That 'y' we are storing into this and then we are printing
'y' and while printing index,
what is the current index value? 0 + 1 = 1. Now, we are printing
1. Right. Now, currently index is 1. Then after this line
what you are doing? Again you are increasing index value
1+1 now, that is 2. That means before going to take third
character you are making your index value as 2, so third character
index is always 2 only, right because 0, 1, 2. So likewise automatically
you are increasing a variable called your required index,
right. So now your required script you got it. Not only
'python', you can take any variable.
Let me run and provide simply "scripting is a good one for
automation". Now see the result.
Space is also one character,
you need to remember that.
Right. See automatically your are getting. For each and every
character you're getting index values.
Right. So, guys if you are able to understand these three lines and
why I have given 'index = 0' before your loop, then
you are good with your loops.
[no audio]
Suppose if I, guys, this logic you can now implement in different
ways, but I am giving just for your, to get your idea on for
loops I am giving in this way this example.
Right. In case if you write 'index = 0' inside of your loop,
then before printing your line always you are making index
value 0. Then even though if you change here, before printing
this line again you are making index value as 0. Always you will get 0.
That's why whenever if you want to initialize any variable
right, initial value, it should be there always before your
loop. That's it.
Now you take even simply some 'python', no problem.
That's it. Right. Okay.
Okay guys, thank you for watching this video.
[no audio]