[英]Not getting desired output by adding character before and after each item in list
我正在尝试从用户处获取多个输入并将其存储在变量中。 存储后,我想添加每个单词的字符开头和结尾,但是我无法在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\'
一些建议:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.