python如何打印带有双引号的字符串列表 [英] python how to print list of strings with double quotes

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

问题描述

我有一个列表,即

my_list = ['a', 'b', 'c', 'd','e','f']

我想以竖线或逗号分隔的格式将其打印出来,但是由于我的某些列表对象可以带有逗号或竖线,因此我想在打印时在每个对象周围用双引号引起来

I want to print this out in a pipe or comma delimited format, but because some of my list objects can have commas or pipes, I would like to wrap double quotes around each object when I print

即输出应该是

"a"|"b"|"c"|"d"|"e"|"f" rather than a|b|c|d|e|f

我无法在我的python版本上使用格式

i can't use format on my version of python

推荐答案

创建一个格式化每个元素的生成器,然后将其解压缩并使用自定义的分隔符.如果您使用的是Python 2,请先使用import print()函数(也可以在Python 3中安全地完成此操作):

Create a generator that formats each element, then unpack it and use a custom separator. If you are using Python 2, import the print() function first (this can be safely done in Python 3 as well):

>>> from __future__ import print_function
>>> print(*('"{}"'.format(item) for item in my_list), sep='|')
"a"|"b"|"c"|"d"|"e"|"f"

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

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