Reputation: 7805
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.
Upvotes: 5
Views: 83040
Reputation: 1771
def add_quote(a):
return '"{0}"'.format(a)
and call it:
a = 'apple'
b = add_quote(a) # output => '"apple"'
Upvotes: 3
Reputation: 2950
You can try this way :
def some_function(a):
b = '"' + a + '"'
return b
if __name__ == '__main__':
a = 'apple'
b = some_function(a)
print(b)
"apple"
Upvotes: 1
Reputation: 560
Beautiful usage in python
b = '"{}"'.format(a)
in python 3.6 (or above)
b = f'"{a}"'
work same!
Upvotes: 24
Reputation: 2095
b = '"' + a + '"'
Notice that I am enclosing the double quotes in single quotes - both are valid in Python.
Upvotes: 7