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. By Shivang Yadav Last updated : September 19, 2023

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.

Example

Consider the below example with sample input and output:

Input:
"learn python programming at https://www.includehelp.com/"

Output:
https://www.includehelp.com/

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.

Syntax

findall(regex, string)

Regular expression to find the URL of a string

The following is the regular expression to find the URL of a string:

(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))

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.

Python program to print URL from a 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])

Output

The output of the above program is:

Enter string : Learn Pythoat https://www.includehelp.com
All URLs are  ['https://www.includehelp.com']

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.