[英]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.