In this project to create a leet speak program with Python you will learn a lot.
Leet (or “1337”), also known as eleet or leetspeak, is a system of modified spellings used primarily on the Internet. (wiki).
We take an input string from the user and convert it to a simplified leet that only converts a few letters. You can extend it further if you like.
This will create a leet speak program with Python and an example input-output pair.
Example
'TAKE ME TO ANOTHER LEVEL'
→ '74K3 M3 70 4N07H3R 13V31'
The first step in creating a project is to break it down into individual steps of what needs to be done.
This helps you as a programmer to keep it simple and not mix things together and create complex solutions. The most common mistake a programmer makes is to try to do too many things at the same time. Instead of just starting to program, you should try to think what is the logical flow that happens.
One solution could be as follows.
This simple breakdown helps you have a simple flow in your program.
The built-in function input() will prompt the user for an input and return it as a string.
phrase = input('Input phrase: ')
This will simply prompt the user and return the input from the user to the variable phrase.
The string method upper() will return an uppercase version of the string.
phrase = phrase.upper()
If the user wrote ‘take me to another level’ then this will return ‘TAKE ME TO ANOTHER LEVEL’.
This is the core of the program and it is great to keep it isolated and not mixed up together with the other steps in the code.
A great way to transform a string into another string is to loop over it and generate it character by character.
The simplest way to make a character conversion is to use a dictionary for that.
lookup = {
'A': '4',
'E': '3',
'I': '1',
'L': '1',
'O': '0',
'S': '5',
'T': '7'
}
leet = ''
for char in phrase:
leet += lookup.get(char, char)
The lookup is a dictionary used to look up a character (the key) and another value (the value of the dictionary).
The loop iterates over each character in the string phrase and uses the dictionary get-method that will return the value if the key exists (the first argument) else it will use the default value (the second argument).
What it does if the character char is, say ‘L’, is to use lookup.get(‘L’, ‘L’) – in this case, it will return ‘1’ because it exists. On the other hand if lookup.get(‘K’, ‘K’) then it would return ‘K’ because ‘K’ does not exist in the dictionary lookup.
Finally, we need to display the result to the user. This can be done by using the print built-in function.
print(leet)
Now you have solved it.
Here you get the full code.
# 1: Input from the user
phrase = input('Input phrase: ')
# 2: Convert input to upper case
phrase = phrase.upper()
# 3: Make the leet conversion
lookup = {
'A': '4',
'E': '3',
'I': '1',
'L': '1',
'O': '0',
'S': '5',
'T': '7'
}
leet = ''
for char in phrase:
leet += lookup.get(char, char)
# 4: Display output
print(leet)
This is part of 19 Python Projects and you can find temperature project converter here and learn 5 essential skills.
Build and Deploy an AI App with Python Flask, OpenAI API, and Google Cloud: In…
Python REST APIs with gcloud Serverless In the fast-paced world of application development, building robust…
App Development with Python using Docker Are you an aspiring app developer looking to level…
Why Value-driven Data Science is the Key to Your Success In the world of data…
Harnessing the Power of Project-Based Learning and Python for Machine Learning Mastery In today's data-driven…
Is Python the right choice for Machine Learning? Should you learn Python for Machine Learning?…