0

I found a project on github that someone put a lot of effort into, my thinking is if they put this much effort into this software, it must work somehow, but I am unable to get it to work.

vprint

def vprint(*args, **kwargs):
    if verbose:
         print(*args, **kwargs)

It has lines like

  vprint("    {file}")

and right about that line is the variable 'file', but it does not print the contents of that variable, it literally prints

      {file}

How do I get python to interpret the variables inside of the brackets?

1
  • vprint is not a built-in function or a well-known extension in python, so you should have given a link to the whole source for one to fully help. That said, you might want to take a look at f-strings. Commented Jul 27, 2019 at 9:23

1 Answer 1

1

What you are looking for is interpolated strings:

print(f"    {file}")

Literal string interpolation was added in Python 3.6 1, 2: such strings are referred to as f-strings, taken from the leading character (f) used to denote such strings, and standing for "formatted strings".

F-strings generally offer an improved, less cumbersome, more flexible and faster way to format strings in python 1, 2, 3. The referenced links include several examples of such strings.

F-strings aren't constant values but expressions that are evaluated at runtime. This allows us to do more powerful formatting, such as inline arithmetic:

a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')

The output would be:

12 multiply 3 is 36.

Sign up to request clarification or add additional context in comments.

5 Comments

Is there some way to enable that? I am using python 3.7.4 and its not processing the variables inside the brackets
vprint is just a wrapper of print, there is nothing to vprint, it just allows you to turn off print... and file is a file retireved from a directory
What result do you have when you call print(file)?
it prints a filepath
If you make sure there is an f in front of the formatting string, then f"{file}" will automatically get turned into a string containing your file name. Try print(f"{file}").

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.