Advertisement

Main Ad

Day 9 of 20: Building a URL Shortener Tool with Python – Step-by-Step Guide

As part of my ongoing 20-day challenge to build one Python project a day, I’m thrilled to present my 9th project, a URL Shortener Tool built entirely in Python.

The goal of this challenge is simple: to push myself daily, improve my Python skills, and build a solid portfolio of small but meaningful projects. This URL Shortener project was both fun and educational, it allowed me to explore how to interact with third-party APIs, handle user input, and develop a simple yet useful command-line application.

In this blog post, I’ll take you through every step I followed to build this tool, from setting up the environment to writing and testing the code.

url shortener

Short URLs are everywhere - from social media platforms to email campaigns. If you've ever wondered how those tiny links work, here's your chance to build your very own URL shortener in Python using the pyshorteners library!

Project Overview

This tool is a command-line Python application that takes a long URL and returns a shortened version using the TinyURL service. It uses the pyshorteners library, which abstracts away the complexity of manually handling API requests.

This is a great beginner-friendly project that involves:

  1. Third-party library usage
  2. Command-line user interaction
  3. Real-world API integration
  4. Functional and clean code organization


Tool/Library Purpose
Python 3.10.7                                         Core programming language
pyshorteners Library to access shortening services
TinyURL API Backend shortening provider
Command Line (CLI) Interface for user interaction
pip Python package installer


Step 1: Project Setup

If you haven't already installed Python on your machine, get it here.

Organize your project by creating a new directory and navigating into it. Then, set up your working environment:

Step 2: Install the Required Library

Before writing any code, we need to install the pyshorteners package - a powerful Python library that provides access to many URL shortening services. To simplify API interactions, we’ll use the pyshorteners library. You can install it with pip by running the following:
Test the installation:
If no error appears, you’re good to go!

Step 3: Creating your URL Shortener Python File

Create a Python file called url_shortener.py inside the url-shortener folder:

Step 4: Writing the Code

Now paste this code into your url_shortener.py file:

Code Breakdown:

  1. pyshorteners is used to access TinyURL.
  2. The Shortener() class initializes the service.
  3. input() prompts the user for a URL.
  4. The shortened link is printed in a clean format.

Code Explanation:

Let’s go through each part of the script:

import pyshorteners
This line imports the pyshorteners library so we can access its built-in URL shortening tools.

shortener = pyshorteners.Shortener()
Here, we create an instance of the Shortener class. This is like creating a "toolbox" that gives us access to different shortening services (like TinyURL, Bitly, etc.).

url = input("Enter URL to shorten:\n")
This will prompt the user to enter the URL they want to shorten. The entered URL is stored in the url variable.

print("New URL:", shortener.tinyurl.short(url))
Finally, we print the shortened URL to the screen so the user can copy and use it.

Step 4: Running the Program

To run your program:

Example output:


My Python Streak – Day 9 of 20

This URL Shortener marks Day 9 of my 20-day Python project streak. Every day, I build a new Python project from scratch - some small, some a bit more complex—to strengthen my skills in:
  1. Writing clean, modular code
  2. Exploring real-world APIs
  3. Experimenting with libraries and tools
  4. Improving my understanding of Python syntax and best practices

Here are just a few of the projects I’ve built so far:

✅ Day 1: Screenshot App
✅ Day 2: Password Generator
✅ Day 3: Email Sender App
✅ Day 4: Chrome Automation
✅ Day 5: Text to Speech
✅ Day 6: WebCam App
✅ Day 7: Screen Recorder App
✅ Day 8: Weather App
✅ Day 9: URL Shortener Tool

Stay tuned for Day 10!

▶️ Demo Video



🌱 Key Takeaways

This project helped me:
  • Learn how to integrate Python with external APIs.
  • Handle user input and output in the command line.
  • Use pip to manage and install packages.
  • Keep code simple and functional.

🚀 Ideas for Future Enhancements

Although this tool is functional, here are some future upgrades:
  1. 🌐 Build a GUI version with Tkinter
  2. 🌍 Add support for Bitly or other services
  3. 📋 Auto-copy shortened URL to clipboard
  4. 🧠 Save link history locally or to a database

📚 Useful Resources

  1. pyshorteners on GitHub
  2. TinyURL API Docs
  3. Python Official Docs

✍️ Final Thoughts
This was a fantastic addition to my daily Python challenge. It's always rewarding to build something you can use, share, or even expand upon later. Projects like this are a reminder that with a few lines of code, you can create something genuinely useful.

Thanks for reading! If you're also working on a coding challenge or learning Python, I’d love to hear from you. Let’s keep building! 🚀

👉 Check out the GitHub Repo
📩 Connect with me on LinkedIn

Post a Comment

0 Comments