Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I tried using the code below, but i still have the ' ' around each element in the string.
l = ['a','b','c'] query = "SELECT " + str(l).strip('[,]') + " FROM table"
returns
"SELECT 'a', 'b', 'c' FROM table"
How can I fix this?
pypika
Using str.join:
str.join
query = "SELECT " + ", ".join(l) + " FROM table" print(query) "SELECT a, b, c FROM table"
As pointed out, forming SQL queries from strings should be a last resort.
Add a comment
l
table
you can use join :-
l = ['a','b','c'] query = "SELECT '" + l.join('') + "' FROM table" print(query) "SELECT 'abc' FROM table"
if you want them to join you can use the above code.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
pypika