0

My code is as below. I am trying to have the str_list to be replaced as '10','11' in the '{}' but what I get is '10,11'. May someone help me to get my desired output?

str_list = '10,11'
self.query= "select ball_name from ball_data where ball_type in ('{}')".format(str_list)

The desired query

"select ball_name from ball_data where ball_type in ('10','11')"

What I currently get is below

"select ball_name from ball_data where ball_type in ('10,11')"

Thanks to all.

2 Answers 2

2

Split it on ,, add the single quotes, then join it back:

self.query = "... in ({})".format(",".join("'" + x + "'" for x in str_list.split(",")))
Sign up to request clarification or add additional context in comments.

Comments

0
str_list = '10','11'
query= "select ball_name from ball_data where ball_type in {}".format(str_list)`

3 Comments

Welcome to StackOverflow. While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
@minglyu in fact it does, the output query is as desired, you can get such definition of str_list easily by str_list = tuple(str_list.split(",")) from OPs one, should be added here tho, this answer seem to be more efficient than accepted to me

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.