Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8191657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:58:00+00:00 2026-06-07T03:58:00+00:00

How do I enclose a variable within single quotations in python? It’s probably very

  • 0

How do I enclose a variable within single quotations in python? It’s probably very simple but I can’t seem to get it! I need to url-encode the variable term. Term is entered in a form by a user and is passed to a function where it is url-encoded term=urllib.quote(term). If the user entered “apple computer” as their term, after url-encoding it would be “apple%20comptuer”. What I want to do is have the term surrounded by single-quotes before url encoding, so that it will be “‘apple computer'” then after url-encoding “%23apple%20computer%23”. I need to pass the term to a url and it won’t work unless I use this syntax. Any suggestions?

Sample Code:

import urllib2
import requests    

def encode():
        import urllib2
        query= avariable #The word this variable= is to be enclosed by single quotes
        query = urllib2.quote(query)
        return dict(query=query)

def results():

    bing = "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query=%(query)s&$top=50&$format=json"
    API_KEY = 'akey'

    r = requests.get(bing % encode(), auth=('', API_KEY))
    return r.json
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:58:01+00:00Added an answer on June 7, 2026 at 3:58 am

    There are four ways:

    1. string concatenation

      term = urllib.quote("'" + term + "'")
      
    2. old-style string formatting

      term = urllib.quote("'%s'" % (term,))
      
    3. new-style string formatting

      term = urllib.quote("'{}'".format(term))
      
    4. f-string style formatting (python 3.6+)

      term = urllib.quote(f"'{term}'")
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to enclose the map within a Fragment . I tried with 2
Is there any way to enclose <style> tags within <body> and still have valid
I have a C++ project with hundreds of files, and I need to enclose
Does it enclose the element within a SPAN element with the given style or
Windows user defined environment variable names can contain any character except = . Special
I've followed this tutorial but when I try to run the application I get
I tried to use acast from reshape2 within a self written function, but had
I am looking to enclose some Oracle components within a Bash script that will
I am trying to enclose two DIV elements, inner-1 & inner-2, (dotted red border)
Is there a way in TCL to enclose a piece of code in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.