What does ${} mean in python code

Hello everyone, I am new here and also new to learning python. I came across this code in databricks notebook and trying to understand what that means. I searched up for ${} in python but it doesn’t seem relevant to the code i’m looking into. Appreciate if someone can please help.

%fs ls ‘${dataset.bookstore}/customers-json’

It looks like shell parameter expansion in Bash (i.e. not Python)

Thanks for the reply, James. Upon digging a bit more into it, it seems like it’s a variable that’s been set within pyspark. Something like this

dataset_bookstore = ‘dbfs:/mnt/demo-datasets/bookstore’
data_catalog = ‘hive_metastore’
spark.conf.set(f"dataset.bookstore", dataset_bookstore)

However, I’m still trying to figure out what “f” means in spark.conf.set(f"…

Hello,

Python has many printing protocols in place. It comes down to choice / preferences:

value = 19.99

print('The price of this item is: $', value, sep='')

# else

print(f'The price of this item is: ${value}')

The ‘f’ allows you to use the ‘{}’ for variable placement.

1 Like

Thank you, Paul.

It doesn’t mean anything. f"dataset.bookstore" is a Python format
string (aka an f-string) which doesn’t have any format elements! This
could have been written "dataset.bookstore".

Normally an f-string contains expressions to insert contained in curly
brackets, eg:

 foo = "abc"
 bah = f"x{foo}x"

The leading f" means that the {foo} should be replaced by the value
of str(foo), so bah should be "xabcx".

The example code you cite does not make use of any {} in the f-string.

1 Like

Bourne shell variable names do no contain dots.

This code:

 %fs ls '${dataset.bookstore}/customers-json'

I believe this is a template syntax.

And ot looks to my like a template for issuing an UNIX ls command with
a badly inserted value from a template variable - it’s subject to an
injection attack because it doesn’t safely quote whatever’s in the
dataset.bookstore template variable. Hopefully that value contains no
single quotes.

1 Like

Welcome Anu,

I want to add to what others have written, and explain that there are many mini-languages both within python as well as near it that can use and reuse symbols and your example is outside python proper.

You said you were working in a notebook environment. The notebook is excuted by first examining cell to see if it is a special command and the percent sign in the first column is a giveaway. Whatever it does may set parameters or do other things that are not a command you can use directly within python when run alone. After such a scan, the remaining code is handed over to a python interpreter.

If you search, you may fine this:

%fs: Allows you to use dbutils filesystem commands. For example, to run the dbutils.fs.ls command to list files, you can specify %fs ls instead. For more information, see Work with files on Databricks.Welcome Anu,

I want to add to what others have written, and explain that there are many mini-languages both within python as well as near it that can use and reuse symbols and your example is outside python proper.

You said you were working in a notebook environment. The notebook is executed by first examining each cell to see if it is a special command meant before python is called on it and the percent sign in the first column is a giveaway.

Python does use the percent sign in various ways but not in the beginning of a statement, so any use there indicates something else is happening. Whatever it does may set parameters or do other things that are not a command you can use directly within python when run alone. After such a scan, the remaining code is handed over to a python interpreter.

If you search, you may find this:

  • %fs: Allows you to use dbutils filesystem commands. For example, to run the dbutils.fs.ls command to list files, you can specify %fs ls instead. For more information, see Work with files on Databricks.

And, in a sense, indeed this seems to be aspects of a shell command showing the files in a folder where the ${...} is interpolated using the value of some variable. The notebook can talk to various slaved processes including whatever “shell” is being used and the python command interpreter and perhaps some for other languages. I am not familiar with Databricks. But ls *folder* is a UNIX-style command in shells like bash that lists the contents of a folder.

%fs ls '${dataset.bookstore}/customers-json'

But as others mentioned, there are many mini-languages either considered part of python or of modules you can import. Some can be fairly complex and many make use of dollar signs and various kinds of braces. Examples include some “sort of” command strings used in some of the printing methods that allow very fine-grained specifications of exactly how you want things printed and regular expressions that allow you to specify exactly what to search for as a pattern.

Anyone can create mini-languages like this for their own purposes and the explanations may often be found outside of python itself. But not always, as some features like f-strings are part of the core now and they too use constructs embedded in text that are evaluated differently.

If you look at your learning environment, you may find other places using the familiar symbols in mysterious ways and sometimes need to find out which one is being used and locate help specific to it. Very often, it will not necessarily be in python. As one example, you may have bit of another language like SQL embedded in strings that will be sent to a database and python has no connection to the contents except to store a character string and hand it over to a function.

1 Like

Right you are.

Apologies for the late read. This explains thoroughly. I realize I have missed looking into the the other file that this statement in question was referring to which is:

dataset_bookstore = 'dbfs:/mnt/demo-datasets/bookstore'
data_catalog = 'hive_metastore'
spark.conf.set(f"dataset.bookstore", dataset_bookstore)

Thanks everyone for your valuable inputs