如何在python中将列表转换为带引号的字符串 [英] How to convert list into string with quotes in python

查看:75
本文介绍了如何在python中将列表转换为带引号的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,希望它是一个带引号的字符串mylist = [1,2,3]

要求 O/P 作为myString = "'1','2','3'"

我试过 mystring = '\',\''.join(mylist)

它给了我结果

mystring = "1','2','3"

缺少第一个和最后一个引号 (')

解决方案

这似乎是迄今为止唯一不是黑客的解决方案......

<预><代码>>>>mylist = [1,2,3]>>>','.join("'{0}'".format(x) for x in mylist)'1'、'2'、'3'"

这也可以更简洁地写成:

<预><代码>>>>','.join(map("'{0}'".format, mylist))'1'、'2'、'3'"

i have a list and want it as a string with quotes mylist = [1,2,3]

require O/P as myString = "'1','2','3'"

i tried mystring = '\',\''.join(mylist)

it gave me result as

mystring = "1','2','3"

first and last quotes (') are missing

解决方案

This seems to be the only solution so far that isn't a hack...

>>> mylist = [1,2,3]
>>> ','.join("'{0}'".format(x) for x in mylist)
"'1','2','3'"

This can also be written more compactly as:

>>> ','.join(map("'{0}'".format, mylist))
"'1','2','3'"

这篇关于如何在python中将列表转换为带引号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆