How to Pass Parameters in URL with Python Last Updated : 16 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Passing parameters in a URL is a common way to send data between a client and a server in web applications. In Python, this is usually done using libraries like requests for making HTTP requests or urllib .Let's understand how to pass parameters in a URL with this example.Example: Python import urllib.parse params = {'name': 'shakshi', 'age': 21} url = 'https://example.com?' + urllib.parse.urlencode(params) # Encoding print(url) Outputhttps://example.com?name=shakshi&age=21 Explanation:This code creates a dictionary with name and age as parameters.It then adds these parameters to the URL using urlencode() to create a complete URL with query strings.Table of ContentPassing Parameters with requestsManually Encoding Parameters with urllibSending Parameters in a POST RequestPassing Parameters with requestsThis requests library allows us to easily send HTTP requests with parameters. By passing parameters through the params argument, they are automatically added and encoded into the URL.Example: Python import requests # Base URL url = 'https://example.com/search' # pass parameter params = {'q': 'python'} # Sending GET request response = requests.get(url, params=params) # Print url with parameter and response print(response.url) print(response.text) Explanation:This code sets a base URL and a parameter q .It sends a GET request with the parameter using requests.get().URL with the parameter and the server's response content are printed.Manually Encoding Parameters with urlliburllib module allows us to manually encode parameters into a URL. We can create a query string by encoding a dictionary and appending it to the base URL. Python import urllib.parse # Base URL url = 'https://example.com/search' # pass parameter params = {'q': 'python'} # Encoding encoded_params = urllib.parse.urlencode(params) # URL with parameter final_url = f'{url}?{encoded_params}' print(final_url) Explanation:This code defines a base URL and a parameter q .It manually encode the parameter into a query string.Encoded parameters are appended to the base URL.Sending Parameters in a POST RequestIn POST request, parameters are sent in the body instead of the URL. This is commonly used for submitting forms or sensitive data like login credentials. Python import requests # Base URL url = "https://example.com/api" # Parameters data = { 'username': 'shakshi', 'password': 'password' } # Sending POST request response = requests.post(url, data=data) # Sending print(response.status_code) Explanation:This code defines the URL and parameters to send in the POST request body.It sends the request with the parameters in the body.Response status code is printed to show the result of the request. Comment More infoAdvertise with us Next Article Pass JavaScript Variables to Python in Flask V vishakshx339 Follow Improve Article Tags : Python python Practice Tags : pythonpython Similar Reads How to Handle Missing Parameters in URL with Flask In this article, we will discuss how to handle missing parameters in URLs with Flask. It is a web framework that offers libraries for creating simple web applications in Python. In Flask, URL parameters, commonly referred to as "query strings," you can organize extra information for a certain URL. P 4 min read How to Download Files from Urls With Python Here, we have a task to download files from URLs with Python. In this article, we will see how to download files from URLs using some generally used methods in Python. Download Files from URLs with PythonBelow are the methods to Download files from URLs with Python: Using 'requests' ModuleUsing 'url 2 min read How to Add URL Parameters to Django Template URL Tag? When building a Django application, it's common to construct URLs dynamically, especially when dealing with views that require specific parameters. The Django template language provides the {% url %} template tag to create URLs by reversing the URL patterns defined in the urls.py file. This tag beco 4 min read Get parameters passed by urls in Django Django is a fully fleshed framework which can help you create web applications of any form. This article discusses how to get parameters passed by URLs in django views in order to handle the function for the same. You might have seen various blogs serving with urls like: -Â www.example.com/articles/ 2 min read Pass JavaScript Variables to Python in Flask In this tutorial, we'll look at using the Flask framework to leverage JavaScript variables in Python. In this section, we'll talk about the many approaches and strategies used to combine the two languages, which is an essential step for many online applications. Everything from the fundamentals to m 7 min read Django return redirect() with Parameters When building web applications with Django, it's common to need to redirect users to different pages, often passing parameters to specify where and with what context the redirect should occur. Django provides a powerful and flexible way to handle redirects using the Django redirect() function, which 3 min read Like