string-interpolation

String interpolation failing inside os.system call

String interpolation failing inside os.system call Question: Editing to skip to the answer: The problem was that the string I was interpolating had a non-ascii character in it that was invisible to the naked eye. I’m trying to run this ImageMagick Command: convert -verbose -size 732×142 xc:transparent -fill black -stroke black -strokewidth 2 -draw "roundrectangle …

Total answers: 2

How do I convert a string into an f-string?

How do I convert a string into an f-string? Question: I was reading this blog post on Python’s new f-strings and they seem really neat. However, I want to be able to load an f-string from a string or file. I can’t seem to find any string method or other function that does this. From …

Total answers: 3

Is there a formatted byte string literal in Python 3.6+?

Is there a formatted byte string literal in Python 3.6+? Question: I’m looking for a formatted byte string literal. Specifically, something equivalent to name = “Hello” bytes(f”Some format string {name}”) Possibly something like fb”Some format string {name}”. Does such a thing exist? Asked By: Enrico Borba || Source Answers: This was one of the bigger …

Total answers: 5

Transform string to f-string

Transform string to f-string Question: How do I transform a classic string to an f-string? variable = 42 user_input = "The answer is {variable}" print(user_input) Output: The answer is {variable} f_user_input = # Here the operation to go from a string to an f-string print(f_user_input) Desired output: The answer is 42 Asked By: François M. …

Total answers: 5

Don't understand this ConfigParser.InterpolationSyntaxError

Don't understand this ConfigParser.InterpolationSyntaxError Question: So I have tried to write a small config file for my script, which should specify an IP address, a port and a URL which should be created via interpolation using the former two variables. My config.ini looks like this: [Client] recv_url : http://%(recv_host):%(recv_port)/rpm_list/api/ recv_host = 172.28.128.5 recv_port = 5000 …

Total answers: 2

f-strings vs str.format()

f-strings vs str.format() Question: I’m using the .format() a lot in my Python 3.5 projects, but I’m afraid that it will be deprecated during the next Python versions because of f-strings, the new kind of string literal. >>> name = "Test" >>> f"My app name is {name}." ‘My app name is Test.’ Does the formatted …

Total answers: 5

psycopg2 difference between AsIs and sql module

psycopg2 difference between AsIs and sql module Question: To choose dynamically a table name in a query I used to use AsIs() from psycopg2.extensions ( http://initd.org/psycopg/docs/extensions.html#psycopg2.extensions.AsIs ), with the following syntax: cur.execute(“SELECT * FROM %s WHERE id = %s;”, (AsIs(‘table_name’), id)) However, the documentation now recommends to use the new psycopg2.sql module available in version …

Total answers: 1

How to postpone/defer the evaluation of f-strings?

How to postpone/defer the evaluation of f-strings? Question: I am using template strings to generate some files and I love the conciseness of the new f-strings for this purpose, for reducing my previous template code from something like this: template_a = “The current name is {name}” names = [“foo”, “bar”] for name in names: print …

Total answers: 14

Getting an "invalid syntax" when trying to perform string interpolation

Why am I getting "invalid syntax" from an f-string? Question: I cannot get f-strings to work in Python 3. I tried this at the REPL: In [1]: state = "Washington" In [2]: state Out[2]: ‘Washington’ In [3]: my_message = f"I live in {state}" File "<ipython-input-3-d004dd9e0255>", line 1 my_message = f"I live in {state}" ^ SyntaxError: …

Total answers: 3

Does Python do variable interpolation similar to "string #{var}" in Ruby?

Does Python do variable interpolation similar to "string #{var}" in Ruby? Question: In Python, it is tedious to write: print “foo is” + bar + ‘.’ Can I do something like this in Python? print “foo is #{bar}.” Asked By: mko || Source Answers: Yes, absolutely. Python, in my opinion, has great support for string …

Total answers: 9