I have met a problem to convert a list: list=['1','2','3']
into ' "1","2","3" '
.
My code is:
str(",".join('"{}"'.format(i) for i in list))
but my output is: "1","2","3"
instead of ' "1","2","3" '
May I have your suggestion? Thanks.
I see you don't want to just prepend/append the needed substrings( the single quote and white space ) to the crucial string.
Here's a trick with str.replace()
function:
lst = ['1','2','3']
result = "' - '".replace('-', ",".join('"{}"'.format(i) for i in lst))
print(result)
The output:
' "1","2","3" '
"' - '"
- acts as a placeholder Or the same with additional str.format()
function call:
result = "' {} '".format(",".join('"{}"'.format(i) for i in lst))
list = ['1','2','3', '5']
result = "' - '".replace('-', ",".join('"{}"'.format(i) for i in list))
# "' - '" - acts as a placeholder
print(result) # ' "1","2","3","5" '
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.