我正在尝试使用单引号将最终输出打印出来,但是无法弄清楚该怎么做。 Kinda是python的新手,所以任何指导我正确使用的指导都将有所帮助。
我尝试在打印函数中将引号与变量连接起来,但出现“语法无效”错误
sample = []
while True:
print ('Enter items into this list or blank + enter to stop')
name=input()
if name == '':
break
sample = sample + [name]
print(sample)
sample.insert(len(sample)-1, 'and')
print(sample)
print('Here is the final output:')
print(*sample, sep = ", ")
最终输出显示类似以下内容: A,B,C和D
但是,所需的输出是: 'A,B,C和D'
答案 0 :(得分:0)
转义如下的引号
print('\'hello world\'')
或使用双引号
print("'hello world'")
答案 1 :(得分:0)
如何先使用join
将列表加入字符串,然后通过string.format
或f-string
在打印中使用该字符串
print('Here is the final output:')
print(sample)
s = ', '.join(sample).strip()
print(f"'{s}'")
输出将为
['A', 'B', 'C', 'and', 'D']
Here is the final output:
'A, B, C, and, D'
f-string
for python3.6
s = ', '.join(sample).strip()
print(f"'{s}'")