Skip to main content
 首页 » 编程设计

python之如何打印字典字符串

2025年05月04日26Free-Thinker

我正在尝试从带括号的原始输入中打印我的字符串。

这是我的代码。

words = (raw_input('Please enter a string:  ')) 
 
names = list(words) 
print names 

我明白了:

['H', 'e', 'l', 'l', 'o'] 

我只需要这样:

[Hello] 

请您参考如下方法:

你不需要 list ,只需要使用 format或 %s :

words = raw_input('Please enter a string: ') 
 
names = '[{}]'.format(words) # or '[%s]'%words 
print names 

如果用户写了多个单词,你可以先拆分输入并打印(注意你需要确保它们之间有空格):

print words.split()