在 Python 中打印字符串时避免使用引号
pythonserver side programmingprogramming更新于 2025/8/3 21:32:17
如果我们按原样打印给定的字符串列表,则必须使用引号并适当地填写一对匹配的引号。我们可以通过以下两种方法避免在打印语句中使用引号。
使用 join()
join 方法可帮助我们使用我们选择的任何分隔符打印列表元素的输出。在下面的示例中,我们选择 ** 作为分隔符。
示例
list = ['Mon', 'Tue', 'Wed']
# 给定列表
print("The given list is : " + str(list))
print("The formatted output is : ")
print(' ** '.join(list))
输出
运行上述代码得到以下结果 −
The given list is : ['Mon', 'Tue', 'Wed'] The formatted output is : Mon ** Tue ** Wed
使用 sep 关键字
sep 关键字也可用于提供格式化的输出,而不必大量使用引号。
示例
list = ['MOn', 'Tue', 'Wed']
# 给定列表
print("The given list is : " + str(list))
print("The formatted output is : ")
print("list, sep =' - '")
输出
运行上述代码得到以下结果 −
The given list is : ['MOn', 'Tue', 'Wed'] The formatted output is : MOn ** Tue ** Wed MOn - Tue - Wed
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

