Check if a String is a URL in Python

  • Post category:String

In this article, you will see how to check if a string is a URL in Python. A URL is the address of a website. Normally, a URL starts with http:// or https://. In Python, you can use this startswith() method to check if a string starts with http:// or https://.

Step 1: Create a string

str = "https://www.codeallow.com"
print("str = ", str)
print("str type = ", type(str))

Step 2: Check if a string is a URL

if str.strip().startswith("http"):
    print("URL")
else:
    print("Not URL")

Output:

str =  https://www.codeallow.com
str type =  <class 'str'>
URL

Free resources to learn advanced skills: AiHints and CodeAllow