How to make a url shortener in python just 5 lines of code

url shortener in python
Table of Contents

Today we will see how to make a url shortner in python it is very simple any one can create it you don’t have to use third party url shorteners now which shows ads and are not free because we will make our own url shortener in python for free.

We will use a python library for this project which is used to shorten links which is free and can be easily installed with pip which is a package manger for python.

Before seeing how to make a url shortener in python if you are intrested in more python projects or other projects than you can visit coderzway projects where I upload more coding projects like this visit coderzway projects.

How to make url shortener in python

So before starting if you don’t have python installed on your machine than you can download it from python website and install it remember to check add python to path – download python.

I will be using vs code to write python code you can use any editor of your choice I will recommend vs code because it is small and fast you can download vs code from here – download vs code.

1. Create a python file

First you need to create a python file with .py extension you can create a file in a seperate folder or anywhere we will just require one python file.

2. Install pyshortener

Now you need to install the python library which will do the most of the work no need to write complex code this library will do everything in the background so you need to install pyshortener

Pyshortener is a url shortening python library which is used to build short links easily and there are lot of api you can use you can browse pyshortener docs here.

To install pyshortener open command prompt or terminal at the file location and type or paste this command.

				
					pip install pyshortener
				
			

3. Copy and paste

Now you have created the file now you need to write the code so you can copy the below code and paste it into the file

				
					#import pyshorteners do pip install pyshorteners to install
import pyshorteners

s = pyshorteners.Shortener()

url = str(input('Enter a url you want to short : '))

print(s.tinyurl.short(url))
				
			

You must be shocked that it is just 5 lines of code that’s a great advantage of python because it has lot of libraries.

You can run the file by typing or pasting the below command

				
					python filename.py
				
			

4. Output

				
					>> Enter the url of the website to short : 
>> https://tinyurl/4930
				
			

Enter the full url you want to shortern it will give you a url which you can paste in the browser and you will be redirected to your main website.

Summary

So this was how to make a url shortener in python it was a simple project with just 5 lines of python code you can explore more and expand open it.

I hope you found what you were looking for and you found this guide useful if you want more guides and projects like this than do subscribe to our newsletter thank you have a nice day.

Share This Post
Share on facebook
Share on twitter
Share on email
Share on whatsapp

This Post Has 2 Comments

  1. vlxx

    Hi, this weekend is fastidious in favor of me, as this occasion i am reading this fantastic
    informative post here at my home.

Leave a Reply

Subscribe To Our Newsletter

Get updates and learn from the best

Latest Guides & Articles