0

I've come across a snippet of Python code that looks like this:

code = 'foo' % bar

...where foo is a string of JavaScript code that's being used in a callback, and bar is a dictionary.

Can someone explain what this means?

1 Answer 1

1

The string is beeing formatted using the old %-formatting style.

The string likely contains sequences like %(foo)s that are being replaced by the dictionary. An example would be:

text = "Hello %(adjective)s World" % {"adjective": "beautiful"}

This would result in Hello beautiful World.

The text inside the parentheses denotes the keyword and the s after that denotes that it is a string. An alternative would be d for integers.

However using this method is not recommended anymore since f-strings and the str.format method provide better functionality.

Read more here.

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.