![](/img/trans.png)
[英]How to convert string elements with single quotes to double quotes in a Python list
[英]how to convert single quotes list with double quotes list
我有一个带单引号的预定义列表,我想在每个元素中使用双引号。
例如,这是我的预定义列表
l = ['A','b']
我只需要作为列表的输出
l = ["A","b"]
我正在尝试使用 json,但它以字符串形式提供列表,但我想要列表。
import json
l = ['A','b']
output = json.dumps(l)
print(type(output))
你的情况没有区别。 尝试打印l
:
l_double = ["A","b"]
l_single = ['A','b']
print(l_double)
print(l_single)
返回
['A', 'b']
['A', 'b']
如果你真的想在你的列表项周围加双引号,试试这样的:
l = ['A','b']
l_real_double = [f'"{c}"' for c in l]
print(l_real_double)
哪个打印
['"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 ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.