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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Turn Your User Into a L33t H4ck3r (Solution)

00:00 Here’s how I would solve this challenge. I opened up a new file and called it translate.py and put in just the overall task, turn user input into leetspeak.

00:10 I’ll add a couple of more, like, subtasks, right? I want to collect user input, and then I want to replace lowercase characters according to rules,

00:27 and then display the output back to the user.

00:32 These are the tasks that I want to solve. The rules are the ones that you have in the task description, so which characters to replace with which one. Let’s start off with collecting the user input.

00:43 User input is again using the input() function, and then a little prompt in here that says, "Enter some text: ".

00:55 And now I want to replace the lowercase characters according to rules. So I’m going to operate on user_input. This is the string that I get from the user, and I will do something with it.

01:06 So keep in mind that strings are immutable, so you can’t really change the user_input, so you might have to always reassign it. So I can say user_input = user_input.replace()

01:18 And then first one was to replace "a" with "4", and make sure to wrap the numbers here also in quotation marks, because this should be a string representation of the number.

01:31 Otherwise, you’re going to run into troubles here.

01:34 Actually, let’s try it out over in the REPL to see what happens. Let’s say I have "aaa", and I want to .replace()

01:45 "a" with 4. So now intentionally I’m not wrapping 4 in quotation marks. And if you try to execute that, you can see that you get a TypeError because .replace() the second argument needs to be a string, not an integer.

01:59 So a very descriptive, nice error message that you get here just tells you that the second argument needs to be a string. So if I change that to a string, then it works.

02:12 Okay? So this is using .replace(). You’ve done it before, and it works. It works just as much with string representations of numbers because it’s just another character.

02:21 And now user_input would have all the "a"s replaced, but you could continue doing that, right? And maybe let’s solve it like this first.

02:29 Whoop, whoop, I need "b", I need "e", I need "l",

02:39 I need "o", I need "s", and I need "t". A belost. Of course, I don’t want to replace them all with a "4", but instead the "b" should be an "8", the "e" should be a "3", the "l" should be a "1", the "o" should be a "0", the "s" should be a "5", and the "t" should be a "7".

03:06 And then finally, I can print the user_input back to the user. So that’s a possible way to solve it. That should work. We’re always replacing the variable user_input with the changed string.

03:20 That is the output of calling .replace() on the initial user_input. Let’s give it a go. Save and run. Enter some text. I like to eat spam and eggs.

03:36 That looks good. I 1ik3 70 347 3gg5 4nd 5p4m. Yeah, it looks like we’re getting the expected result, so that’s great. That’s a possible solution. It looks a little clunky.

03:46 There’s a lot of repeated text in here. In the next lesson, I’m going to show you a way to clean this up a little more, but if you’ve come to this solution, that’s totally fine as well.

Become a Member to join the conversation.