Python program to print URL from a string

Here, we will take user input for a string and then print the URL from the string if it exists in the string in Python programming language.
Submitted by Shivang Yadav, on April 28, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Strings in Python are immutable means they cannot be changed once defined.

URL stands for Uniform Resource Locator also known as web address is the location of a website on the network.

Print URL from String

We will take a string as input from the user. From this string, we will extract the URL from the user and print it, if it exists otherwise print 'no URL exists'.

This is done using regular expressions.

Regular Expression (RegEx) is a sequence of characters that defines a search pattern.

Python Regular Expression Programs


"learn python programming at"


Python program to print URL using string

We will find the URL from the string using the regular expression which is made to accept URLs from the given strings.

We will use the findall() method from python's RE library.


findall(regex, string)

Regular expression to find the URL of a string


This large expression checks for all possible combinations of the URL. starting from character followed by http_: … and then . followed by the top level domain.

Program to print URL from the string

# Python program to print URL from a string
import re

# Getting strings as input from the user 
myStr = input('Enter string : ')

# Finding all URLS from the string 
urlRegex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»""'']))"

allUrls = re.findall(urlRegex,myStr)	

print("All URLs are ", [url[0] for url in allUrls])


Enter string : Learn Pythoat
All URLs are  ['']

Python String Programs »

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.