Problem 6
Emojize
Write a program that turns emoji codes like :thumbs_up: into real emoji.
Background
Lots of apps let you type a short code like :thumbs_up: and have it turn
into an emoji (š) on its own. Some codes even have shorter aliases, so
:thumbsup: (no underscore before āupā) works too.
Youāll write a program that does this conversion yourself, using a
function from a package called emoji.
Getting Started
Log into cs50.dev, click on your terminal window, and run:
cd
mkdir emojize
cd emojize
code emojize.py
That creates a new folder called emojize, moves into it, and opens a
new, empty file called emojize.py for you to edit.
Next, create and activate a virtual environment. This is the first problem in this pathway that needs a package from outside Python itself, and a virtual environment keeps that package (and any others you install later) scoped to this one project instead of your whole account:
python -m venv myvenv
source myvenv/bin/activate
python -m venv myvenv creates a folder holding its own private
copies of python and pip. source myvenv/bin/activate doesnāt
install anything, it just points your terminal at those private
copies instead of the ones shared across your whole account, for the
rest of this terminal session. Youāll know it worked because your
prompt starts showing (myvenv).
Thereās a matching deactivate command that switches your terminal
back to the shared copies, but youāll rarely use it. Once youāre
working on a project, the normal thing is to just leave the virtual
environment active for as long as youāre working in that terminal.
Closing the terminal has a similar effect, except then youāll need to
cd back into emojize and run source myvenv/bin/activate
again next time, not deactivate.
Now install the emoji package, which does the actual code-to-emoji
lookup for you:
pip install emoji
Specification
Design and implement a program, emojize.py, that prompts the user for
a string of text and prints an āemojizedā version of it.
- Your program must prompt the user for a string of text with
input(). - Use the
emojipackageāsemojizefunction to convert the string, and print the result. - Call
emojizewithlanguage="alias"so it recognizes English codes and their aliases, not codes written in another language.
from emoji import emojize
print(emojize("very funny :thumbs_up:", language="alias"))
Thatās the whole program: one input(), one call to emojize, one
print(). The hard part isnāt the code, itās knowing what to type
inside the colons, which is what the next section is for.
Browse Emoji Codes
CS50ās own version of this problem set points you at a reference page
of every code and alias the emoji package understands. That page is
blocked by the school firewall. Hereās the same list, generated
straight from the same package you just installed, hosted on its own
page so it doesnāt turn this lesson into a 5,000-row scroll:
Search the emoji code list ā
Usage
Your program should behave like the demo below.
$ python emojize.py Input: :1st_place_medal: š„ $ python emojize.py Input: :money_bag: š° $ python emojize.py Input: :smile_cat: šø $ python emojize.py Input: nice job on that pset :thumbs_up:, cs50 is :fire: nice job on that pset š, cs50 is š„
A code emojize doesnāt recognize is left exactly as it was typed,
colons and all, since thereās no emoji to replace it with.
Hints
emojize only changes the parts of the string written like
:this:. Anything else in the string, including the spaces between
words, comes through unchanged. That means you donāt need to find or
replace anything yourself, your whole program is the one input()
call, the one emojize() call, and the one print().
If a code prints as :like_this: instead of turning into an emoji,
double-check the exact spelling using the search box above. A single
missing underscore (:thumbsup: is a real alias, but :thumbs up:
with a space is not) is the most common reason it doesnāt match.
Need a hint?
If some codes convert and others donāt, check the language argument
before anything else. emojize() defaults to language="en", which
only matches each emojiās one canonical name, not its aliases.
:thumbs_up: is a canonical name, so it converts even if you forget
the argument entirely. But :smile_cat: and :thumbsup: are aliases,
not canonical names, so they silently print back unchanged unless you
pass language="alias", same as the Specification section above
shows.
Style and Submission
Run these one at a time, from inside your emojize folder. If youāre
coming back to this in a new terminal, reactivate your virtual
environment first (source myvenv/bin/activate) or check50 will
report a missing emoji module even though your code is correct.
Check your style:
style50 emojize.py
Check your correctness:
check50 porttack/cs50/problems/py/emojize
Submit your work:
submit50 porttack/cs50/problems/py/emojize
Glossary
- package ā Code someone else wrote that you can import into your
own program, instead of writing it yourself.
emojiis a package;pip installis how you get a copy of it onto your computer. - function ā A named, reusable block of code.
emojize()is a function: you hand it a string, it hands back a new one. - parameter ā A value a function accepts as input, named in its
definition.
emojizeāslanguageparameter controls which set of codes it recognizes. - alias ā A second, usually shorter, name for the same thing. Many
emoji have one;
:thumbsup:is an alias for:thumbs_up:.
Version history
- Loading commit history…