Hi, everyone.
In today's example, we're going to transform this list of file names into this.
So that's the output of the program.
So let's suppose we had some files and we got their file names using some other functions, which we'll
learn later on in the course.
And those functions created a list.
And then you want to first change these strings into the new names.
And for that you use a list comprehension, which we will do now.
And then maybe later on, you reassign those strings to the existing files on disk.
So today we're simply going to work on transforming the strings.
So we got these names, the existing names of the files.
Now we want to transform them.
So when you want to transform a list, what you think is a list comprehension.
So let's write the list comprehension.
We will replace we will overwrite the existing variable with the new list, which is constructed by
the list comprehension.
So what do we want to do to the file names?
You see, this is one dot, doc, and we want to transform it to one dash doc plus dot text.
So how do we replace the dots with a dash?
Well, using that replace.
So file name is a string.
In the first iteration, it represents the first string.
So that expects a string, which means the character you want to replace.
And another string, the character that you want the existing character to be replaced with.
So the new list is going to contain.
The string with that character replaced, plus another string, which is the text.
So that will construct a new string.
Because it is possible to.
Concatenate two strings so you get.
Hey there.
That's what we're doing there.
For file name in file names, print, file names, run.
And here is the result.
So that's the final code.
That is the syntax of the list comprehension.
So it always starts with the new version of each item.
So the new version of each item is this.
The character replaced.
Plus this added to the.
New string.
And you do that for each file name in the file names list.
So thanks for following.