Add element with single quote to dictionary in Python [duplicate] - python

This question already has answers here:
Using quotation marks inside quotation marks
(12 answers)
Closed 6 years ago.
I created dictionary in python like below:
flip= {'0': 'a"', '1':'b"', '2':'c"'}
But I don't want to use double quotes. I need elements with single quotes.
How can I do something like below? I was trying with \\, \, but it seems not to work.
Correct dict should look like below:
flip= {'0': 'a'', '1':'b'', '2':'c''}

You can simply use double quotes around the element.
lip= {'0': "a'", '1':"b'", '2':"c'"}

Using backslash should work, have you tried below?
flip= {'0': 'a\'', '1':'b\'', '2':'c\''}

Related

How to force a dictionary to be written with " " instead of ' ' [duplicate]

This question already has answers here:
Convert single quotes to double quotes for Dictionary key/value pair
(2 answers)
Closed 10 months ago.
So I am working on a project in which I build an ongoing dictionary for kennings (its for a norse mythology course). The problem I run into is on occasion a kenning has an apostrophe in it. For example the kenning "wolf's joint" which has the definition "joint" gets written to the dictionary as {"wolf's wrist": 'joint'}, this would be fine if the json.loads() function didn't through up an error because the key has "" and the value has ''. I was wondering if there is a way to force a dict to be written always with the "" instead of ''.
You can try json.dumps:
>>> print(json.dumps({"wolf's wrist": 'joint'}))
{"wolf's wrist": "joint"}

Put single and double quote in Python string format? [duplicate]

This question already has answers here:
Python string literals - including single quote as well as double quotes in string
(3 answers)
Using quotation marks inside quotation marks
(12 answers)
Closed 3 years ago.
I'm having trouble figuring out how to print a very specific line.
I tried using .format and I also tried the print("str",variable,"str") method but I can't seem to figure out how to make it print correctly.
feet=5
inches=6
print('Room Length:{}' {}"'.format(feet,inches))
I want the computer to print; Room Length: 5' 6"
but I am not sure how to print this while keeping the apostrophe and the quotations for feet and inches.
print('Room Length:{}\' {}\"'.format(feet,inches))

Removing single quotes from a list of string [duplicate]

This question already has answers here:
I want to replace single quotes with double quotes in a list
(3 answers)
Closed 3 years ago.
I have a list of products which are strings & want to remove single quotes from it
The list of products retrieved from a sql query in python environment
['000003286694', '000003286704', '000003420412']
I have to pass this list as a payload to json object. So it needs to be preceded by double quotes.
I have used following method
d=[('"'+i+'"') for i in x]
but I get the output as
['"000003286694"', '"000003286704"', '"000003420412"']
I tried using regex, replace method to get away with single quotes but no success.
Can anyone please share workaround for this
I want result list should be
["000003286694", "000003286704", "000003420412"]
data =['000003286694', '000003286704', '000003420412']
import json
result =json.dumps(data)
print(result) #["000003286694", "000003286704", "000003420412"]

escaping {} brackets in triple quote Python strings [duplicate]

This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 3 years ago.
how can you format a string of this form in Python 3?
'''{name}{{name}}'''.format(name="bob")
the desired output is: bob{bob}, but the above gives: bob{name}.
one solution is to add another argument to format:
'''{name1}{name2}'''.format(name1="bob", name2="{bob}")
but this is excessive. is there a way to properly escape { such that inner {x} can still be interpolated and one can only pass a single name to format?
Add one more level of {}:
'''{name}{{{name}}}'''.format(name="bob")
which outputs:
bob{bob}

Weird Behavior of f-strings and print when used together in Python [duplicate]

This question already has answers here:
Using quotation marks inside quotation marks
(12 answers)
Closed 5 years ago.
I'm using Python 3.6. I feel the below code is acting bit weird when f-strings and print statement are used together in Python
person = {"name": "Jenne", "age": 23}
print(f"Person name is {person["name"]} and age is {person["age"]}")
The above statement results in Error
But when double quotes are replaced with single quotes in the print statement across name and age then it works like a charm.
print(f"Person name is {person['name']} and age is {person['age']}")
Can anyone please explain for this behavior?
The double-quoted "name" conflicts with the double-quoted outer string f"Person name is {..." Which is to say that the first double-quote of "name" ends the previous string. You can't nest strings this way.
N.B. that f'Person name is...' or f'''Person name is...''' or f"""Person name is...""" would all work with double quotes.

Categories