Refactor to Use Method Chaining
00:00 In the previous lesson, you solved the task of converting user input to leetspeak. Now as said, I have a little bonus of just trying to clean this up a bit to make it less repetitive code and a little better readable.
And you can do that by using method chaining again, like I’ve shown you in a previous lesson, like this: calling
.replace() returns the string, which means that you can call another
.replace() call on that returned string, which is essentially what you’re doing here, just here you’re assigning it to a variable and then calling it on that new variable.
I like to eat, just going to stick with that.
I like to eat still works. And now another way of cleaning this up a bit more if you use method chaining like that is you can use the backslash character (
\), which tells Python to continue, place it before a dot, then Python knows to continue the line.
01:14 So you’re escaping the newline here, and that’s just a special way of writing. It just tells Python that you’re continuing. So this is essentially the same as writing this long line that I just created before, but it makes it a bit more readable, still works as before. Save and run.
02:09 and finished the script and also finished the course. So in the next lesson, we’ll do a quick recap of things that you’ve practiced in this course, and then I’ll give you a couple of additional resources where you can continue your learning in the final lesson.
Become a Member to join the conversation.