Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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.

00:13 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.

00:30 But that creates a lot of repetition here. And you can do this. You could just keep chaining them at the end.

00:41 That’ll be a deterioration in readability though, because now we’re getting a really long line. I just want to show you that it works.

00:50 I can run that script again, and it should work just the same as before.

00:56 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.

01:34 I 1ik3 70 347.

01:37 And with that, you’ve gotten rid of a lot of the duplication here. You’re not constantly reassigning user_input to the output of .replace() on the previous version of user_input.

01:49 But in that case, you’re just chaining the .replace() methods on top of each other and then assigning it to a new version of user_input and then printing out that new string, user_input.

02:03 I think this looks a bit more readable than before. And with that, I’m happy

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.

Avatar image for markusurbauer

markusurbauer on Jan. 10, 2024

Better solution ;)

collect user input

user_input = input("Enter some text:")

replacement dictionary

dictionary = {"a":"4",
              "b":"8",
              "e":"3",
              "l":"1",
              "o":"0",
              "s":"5",
              "t":"7"}
for key,value in dictionary.items():
    user_input = user_input.replace(key,value)

display output

print(user_input)

Become a Member to join the conversation.