GithubHelp home page GithubHelp logo

url-shortener-in-python's Introduction

How to Create URL Shortener in Python?

How to Create URL Shortener in Python?

Hello Readers! So, you would have seen short URLs being used in various places (social media, websites, messaging platforms, etc.). Short URLs are easy to remember or type so they are very popular.

No one loves long URLs and so the need to shorten lengthy URLs often comes to us.

We can write a program in Python language for our needs. Then we can give a long URL as the input and get short URLs as output, too in very few lines of code. Is not it exciting? The use of various APIs does it very easily without having to dig into complex topics.

We're going to use a library named PyShorteners, which i think is the best library to shorten your URLs in Python. Let's begin the journey.

What is a URL Shortener?

A URL shortener is a simple tool that takes a long URL and turns it into a short url which can be easily, remembered by an individual.

What is PyShorteners?

PyShorteners is a Python library to help you short urls, it is the most . You just have to install the lib and you're good to proceed.

You can install the lib using the command given below.

pip install pyshortners

After installing the PyShortener library let's start the Implementation part.

Implementation

Here we have to import the PyShorteners Library before using it, if you have used python before you should can relate to it, but those who don't know about what we are talking about let's know that first.

In the first line of our code we have to import the library which we installed before, in order to use it's features.

Import the PyShorteners library using the code given below.

import pyshorteners

Once you have imported pyshorteners. Our next step is to create a variable named "URL" which is basically a variable, going to store our link which is going to be shortened. The link will be in double quotes as it is a string.

URL = "Paste your link here"

Next we're going to create another variable named "short_URL" which will call pyshorteners library, to shorten the url.

short_URL = pyshorteners.Shortener()

In this step we're going to do the main step, we are going to make a variable named "result" and then we are telling pyshorteners to shorten the link which is in a variable named "URL", which we created earlier. Here "tinyurl" in the code is the website which is going to shorten the URL.

result = short_URL.tinyurl.short(URL)

We have almost completed all the steps, in this last step we are going to print the URL which is shortened.

print("Shortened URL: " + result)

All complete here, is our final code.

import pyshorteners # Imported Pyshorteners

URL = "Paste your link here" # Making a variable, whichj will store the link.

short_URL = pyshorteners.Shortener() # Calling the function to do the work.

result = short_URL.tinyurl.short(URL) # Telling the function the location of the link.

print("Shortened URL: " + result) # Printing the output.

Now, here is our output.

Output - How to Create URL Shortener in Python?

Peace โœŒ๏ธ

url-shortener-in-python's People

Contributors

dharmicdeveloper avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.