简体   繁体   中英

how to convert single quotes list with double quotes list

I have a predefined list with single quotes and I want double quotes in each element.

for example, this is my predefined list

l = ['A','b']

the output I needed as the list only

l = ["A","b"]

I am trying with json but it is giving list as a string but I want list.

import json
l = ['A','b']
output = json.dumps(l)
print(type(output))

There is no difference in your case. Try to print l :

l_double = ["A","b"]

l_single = ['A','b']

print(l_double)

print(l_single)

returns

['A', 'b']
['A', 'b']

In case you really want double quotes around you list items, try something like this:

l = ['A','b']

l_real_double = [f'"{c}"' for c in l]

print(l_real_double)

which prints

['"A"', '"b"']
L = ['A', 'B', 'C']
print('[', end='') #just to print the opening third bracket [
for i in range(len(L)): #the variable i will assume the values from 0 to length of L - 1 i.e. the indices of the elements
    print('"' + L[i] + '"', end='') #display a double quote and the string inside it
    if i < len(L)-1:
        print(',', end=' ') #if i is not the index of the last element then display a comma after it
print(']') #just to print the closing third bracket ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM