One of the cool things about Python is its simplicity. Often, with Python, we can accomplish more with less. Here is a quick example: To print "Hello World" in Python, it only takes this simple line of code:
Here is how we can accomplish this in Java:
While both codes do the same thing, the Python version is just one line. There are no class declarations, no main method, and no boilerplate. This is one of the reasons Python is so popular for beginners and loved by professionals. In this article I want to focus on the practical things that you can accomplish with Python with just a few lines of code. Here is what we are going to explore:
Creating QR codes
Convert text to speech
Translate text automatically
Take screenshots
Censor profanity in text
Let's get started!
Build the Confidence to Tackle Data Analysis Projects [30% OFF]
Ready to go in and do some real data analysis? The main purpose of this book is to ensure that you develop data analysis skills with Python by tackling challenges. By the end, you should be confident enough to take on any data analysis project with Python. Start your journey with "50 Days of Data Analysis with Python: The Ultimate Challenge Book for Beginners."
Other Resources
Want to learn Python fundamentals the easy way? Check out Master Python Fundamentals: The Ultimate Python Course for Beginners
Challenge yourself with Python challenges. Check out 50 Days of Python: A Challenge a Day.
100 Python Tips and Tricks, Python Tips and Tricks: A Collection of 100 Basic & Intermediate Tips & Tricks.
Create QR Codes
Do you want to generate QR codes for URLs or text? Well, Python has a perfect library for that task. This library is known as qrcode. You can easily install the library using pip. Just run:
pip install qrcode
Once you have the library installed, you have to just import it into your script and use it with just a few lines of code:
This will output:
You can see that with just a few lines of code, we have created a QR code for a URL. You can achieve the same results not just with URLs but with text too. In this code, we are using the PIL library to view the generated image.
Convert Text to Speech
Do you want to convert text to speech? Python has got your back. Python has a library called pyttsx3 that lets you convert any text into speech. It works offline and is super easy to use. First, you can easily install it using pip:
pip install pyttsx3
Now that it is installed on your machine, import it into your script and use it:
In this script, first we import the pyttsx3 library. The pyttsx3 engine is initialized using pyttsx3.init(). This function takes a string (Hello, welcome to the world of Python. Learn Python and be great!) as input and saves it as an MP3 file and speaks the given text aloud. The script uses engine.say(text) to queue the text for speech output. The engine.runAndWait() is called to process and play the speech. Try it out!
Translate Text Automatically
Do you have text that you need to translate using Python? Well, you can use the deep-translator library. This library can translate text using multiple providers (Google, Libre, etc.). It is 100% free, unlimited, and easy to use. First, you need to install it using pip:
pip install deep-translator
Here is a simple example of how you can use it:
Look at that! With just a few lines of code, we have translated text from English to French. There is a lot more you can do with the library. Click here to explore more functionality.
Taking Screenshots with Python
Do you want to take a screenshot? Python has a library called Pyautogui. This library is useful primarily for automating graphical user interface (GUI) tasks. It allows you to simulate user actions, such as clicking buttons, typing text, or moving the mouse cursor. Did you know that with just a few lines of code, it can take a screenshot of your computer? Before you use this library, you have to install it on your computer. You can install it using pip:
pip install pyautogui
If you run the script below, it will take a screenshot of your computer, and it will save an image in the same directory as your Python script.
Censoring Profanity in Text
Sometimes, when you are working with texts, you may want to censor certain words that you deem inappropriate. The Python Better Profanity library is a great tool to censor offensive language in your text. It's easy to install and use, and it provides powerful functionality to clean up text. First, install the module using pip:
pip install better_profanity
Here’s a sample text with profanity that we want to censor:
You can see here that the library detects the offensive words in the text and censors them. By default, Better Profanity uses an asterisk (*) to censor bad words. However, we can customize the censor character. See below:
We can also use the library to check if a string contains a bad word using the contains_profanity method. This method will return a boolean value of True if a string has offensive words and False if it does not. See below:
This is not all. We can also create a custom list of words that we want to censor. If those words are in the string, then they will be censored. Below, we want the word 'code' to be censored:
You can see that only the word 'code' has been censored above.
Wrap-Up
I think we’ll stop here. As you can see, everything we’ve done was accomplished with just a few lines of code. I call this the "straight to the point" type of code. No rumbling around here. The examples also highlight another advantage of using libraries: by leveraging code that others have already written, we avoid starting from scratch. This saves time and makes our code more concise.
These are just a few things you can do with Python to handle everyday tasks. Python isn’t just a programming language. It’s a powerful tool that can simplify your daily life and boost your productivity. Thanks for reading.