Skip to content
Advertisement

unable to get desired output using string format in python

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.

Advertisement

Answer

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

self.query = "... in ({})".format(",".join("'" + x + "'" for x in str_list.split(",")))
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement