在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
热门推荐
10 新年的生日祝福语简短
11 高中升学宴祝福语简短
12 老师简短祝福语有哪些
13 老同学十一祝福语简短
14 家人毕业祝福语简短励志
15 新年给妈妈简短祝福语
16 祝福语勤奋大全简短英语
17 姥爷生日祝福语简短精辟
18 妈妈生日祝福语简短温暖