我正在尝试从用户那里获取多个输入并将其存储在变量中。存储之后我想添加每个单词的开头和结尾,但是我无法在python 3.X中获得所需的输出
我的代码如下: -
string_value=[str(i) for i in raw_input("Enter space separated inputs: ").split()]
aces = ["\'" + string_value + "\'" for string_value in string_value]
print (aces)
输出: -
Enter space separated inputs: John Sunil Kathylene Bob
["' John ' ", " ' Sunil ' ", " ' Kathylene ' ", " ' Bob' "]
所需的输出: -
\'John\',\'Sunil\',\' Kathylene\',\' Bob\'
答案 0 :(得分:0)
有人建议:
str
到str
的类型转换是多余的。代码:
words = input("Enter space separated inputs: ").split()
prefix_and_suffix = "\\'"
formatted_words = ','.join('{0}{1}{0}'.format(
prefix_and_suffix, word) for word in words)
print (formatted_words)