I have a list containing number and string:
words = ['hello', 2, 3, 4, 5]
I want a string:
"'hello', 2, 3, 4, 5"
What's the easiest way to do this with Python
I have a list containing number and string:
words = ['hello', 2, 3, 4, 5]
I want a string:
"'hello', 2, 3, 4, 5"
What's the easiest way to do this with Python
words = ['hello', 2, 3, 4, 5]
", ".join([str(x) if isinstance(x,int) else "'{}'".format(x) for x in words])
Output:
"'hello', 2, 3, 4, 5"
One way using str.strip
:
str(words).strip("[]")
Output:
"'hello', 2, 3, 4, 5"
If i understand you, this is my solution:
words = ['hello', 2, 3, 4, 5] result = '''"{}"'''.format(", ".join(map(lambda x: "'{}'".format(str(x)) if isinstance(x, str) else str(x), words))) print(result)
Output is:
"'hello', 2, 3, 4, 5"
Do this:
words = ['hello', 2, 3, 4, 5]
print(','.join([str(elem) if isinstance(elem,int) else "'{}'".format(elem) for elem in words]) )
Output :
'hello',2,3,4,5