在Python中打印字符串时避免引号
如果按原样打印给定的字符串列表,则必须使用引号并适当地填写一对匹配的引号。我们可以通过以下两种方法避免在打印语句中使用引号。
使用join()
join方法可以帮助我们使用选择的任何分隔符来打印列表元素的输出。在下面的示例中,我们选择**作为分隔符。
示例
list = ['Mon', 'Tue', 'Wed'] # The given list 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'] # The given list 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