2

I have two variables:

query = "String: {} Number: {}"
param = ['text', 1]

I need to merge these two variables and keep the quote marks in case of string and numbers without quote marks.

result= "String: 'text' Number: 1"

I tried to use query.format(param), but it removes the quote marks around the 'text'. How can I solve that?

1 Answer 1

7

You can use repr on each item in param within a generator expression, then use format to add them to your string.

>>> query = "String: {} Number: {}"
>>> param = ['text', 1]
>>> query.format(*(repr(i) for i in param))
"String: 'text' Number: 1"
Sign up to request clarification or add additional context in comments.

Comments

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.