5

I am using Python 2.6+ and would like to append double quotes to a string and store it as a new string variable. I do not want to print it but use it later in my python script.

For example:

a = 'apple'
b = some_function(a) --> b would be equal to '"apple"'

How can I do this? All the solutions that I have looked at so far only work when printing the string.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
activelearner
  • 7,055
  • 20
  • 53
  • 94

4 Answers4

23

Beautiful usage in python

b = '"{}"'.format(a)

in python 3.6 (or above)

b = f'"{a}"'

work same!

Jiun Bae
  • 550
  • 3
  • 14
6
b = '"' + a + '"'

Notice that I am enclosing the double quotes in single quotes - both are valid in Python.

perigon
  • 2,160
  • 11
  • 16
3
def add_quote(a):
    return '"{0}"'.format(a)

and call it:

a = 'apple'
b = add_quote(a) # output => '"apple"'
Hadi Farhadi
  • 1,773
  • 2
  • 16
  • 33
1

You can try this way :

def some_function(a):
    b = '"' + a + '"'
    return b

if __name__ == '__main__':
    a = 'apple'
    b = some_function(a)
    print(b)

Output:

"apple"
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45