如何在python中打印不带[,';';的列表

如何在python中打印不带[,';';的列表,python,Python,我想打印 *IBM is a trademark of the International Business Machine Corporation. 而不是用python ['*', 'I', 'B', 'M', ' ', 'i', 's', ' ', 'a', ' ', 't', 'r', 'a', 'd', 'e', 'm', 'a', 'r', 'k', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 'I', 'n', 't', 'e', 'r', '

我想打印

*IBM is a trademark of the International Business Machine Corporation.
而不是用python

['*', 'I', 'B', 'M', ' ', 'i', 's', ' ', 'a', ' ', 't', 'r', 'a', 'd', 'e', 'm', 'a', 'r', 'k', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 'I', 'n', 't', 'e', 'r', 'n', 'a', 't', 'i', 'o', 'n', 'a', 'l', ' ', 'B', 'u', 's', 'i', 'n', 'e', 's', 's', ' ', 'M', 'a', 'c', 'h', 'i', 'n', 'e', ' ', 'C', 'o', 'r', 'p', 'o', 'r', 'a', 't', 'i', 'o', 'n', '.']
我的代码:

n=str(input())
l=len(n)
m=[' ']*l
for i in range(l):
    m[i]=chr(ord(n[i])-7)
print(m)
使用
join

x = ['*', 'I', 'B', 'M', ' ', 'i', 's', ' ', 'a', ' ', 't', 'r', 'a', 'd', 'e', 'm', 'a', 'r', 'k', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 'I', 'n', 't', 'e', 'r', 'n', 'a', 't', 'i', 'o', 'n', 'a', 'l', ' ', 'B', 'u', 's', 'i', 'n', 'e', 's', 's', ' ', 'M', 'a', 'c', 'h', 'i', 'n', 'e', ' ', 'C', 'o', 'r', 'p', 'o', 'r', 'a', 't', 'i', 'o', 'n', '.']
print(''.join(x))
'*IBM is a trademark of the International Business Machine Corporation.'

假设您的列表是:

this_is_a_list = ['*', 'I', 'B', 'M', ' ', 'i', 's', ' ', 'a', ' ', 't', 'r', 'a', 'd', 'e', 'm', 'a', 'r', 'k', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 'I', 'n', 't', 'e', 'r', 'n', 'a', 't', 'i', 'o', 'n', 'a', 'l', ' ', 'B', 'u', 's', 'i', 'n', 'e', 's', 's', ' ', 'M', 'a', 'c', 'h', 'i', 'n', 'e', ' ', 'C', 'o', 'r', 'p', 'o', 'r', 'a', 't', 'i', 'o', 'n', '.']
使用
加入

''.join(this_is_a_list)
扩展:

如果您计划在将来使用
字符串
:这种方法效率极低,但我将把它留在这里作为一个展示不该做什么的例子:(感谢@PM 2Ring)

进一步编辑,感谢@kuro

你可以试试这个

to_print = ['*', 'I', 'B', 'M', ' ', 'i', 's', ' ', 'a', ' ', 't', 'r', 'a', 'd', 'e', 'm', 'a', 'r', 'k', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 'I', 'n', 't', 'e', 'r', 'n', 'a', 't', 'i', 'o', 'n', 'a', 'l', ' ', 'B', 'u', 's', 'i', 'n', 'e', 's', 's', ' ', 'M', 'a', 'c', 'h', 'i', 'n', 'e', ' ', 'C', 'o', 'r', 'p', 'o', 'r', 'a', 't', 'i', 'o', 'n', '.']
word = ''
for i in range(len(to_print)):
    word = word + to_print[i]

print (word)

执行此操作的明智方法是使用
.join
。要执行解码操作,您可以直接在输入字符串的字符上循环,而不是使用索引

s = input('> ')
a = []
for u in s:
    c = chr(ord(u) - 7)
    a.append(c)
print(''.join(a))
演示

> 1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
*IBM is a trademark of the International Business Machine Corporation.
通过使用列表,我们可以使其更加紧凑

s = input('> ')
print(''.join([chr(ord(u)-7) for u in s]))

为什么这是一个列表?它来自哪里?
print(*your_lst,sep='')
如果您使用的是Python 3.BTW,那么您可能会获得这些否决票,因为1.您没有向我们展示您自己的代码尝试,2.您没有回复要求您解释此列表来源的评论。最好的解决方案可能是修复上游内容,以便在第一个p中以字符串而不是列表的形式获取数据lace.Code添加了@PM2Ring:)错误:“”。联接(x)类型错误:序列项0:应为str实例,列表found@ToufiqueImam“.join(x)注意两个引号不能
s=”.join(这是一个列表)
做这项工作?正如kuro所说。第二个版本效率极低。事实上,这是一个很好的例子,说明了如何不做这项工作。;)当所需要的只是公认的python式做事方式时,超越了顶层代码:join()请参阅我对DarkKnight答案的评论。这是一种非常不符合Python的解决问题的方法。在Python中,我们通常直接在列表项上循环,而不是通过索引间接循环:这样更高效,生成的代码更干净。在某些情况下,您确实需要索引和项,但在这种情况下,您通常可以使用
enumerate
> 1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
*IBM is a trademark of the International Business Machine Corporation.
s = input('> ')
print(''.join([chr(ord(u)-7) for u in s]))