简体   繁体   English

通过在列表中的每个项目之前和之后添加字符,无法获得所需的输出

[英]Not getting desired output by adding character before and after each item in list

I am trying to take multiple input from a user and store it in a variable. 我正在尝试从用户处获取多个输入并将其存储在变量中。 After storing I want to add character starting and ending of each word, but I am failing to get desired output in python 3.X 存储后,我想添加每个单词的字符开头和结尾,但是我无法在python 3.X中获得所需的输出

My code is as follows:- 我的代码如下:

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)

Output:- 输出: -

Enter space separated inputs: John Sunil Kathylene Bob

["' John ' ", " ' Sunil ' ", " ' Kathylene ' ", " ' Bob' "]

Desired Output :- 所需输出:-

\'John\',\'Sunil\',\' Kathylene\',\' Bob\'

Some advise: 一些建议:

  • Read PEP8. 阅读PEP8。
  • Type-casting of str to str is redundant. strstr类型转换是多余的。

Code: 码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM