+10 XP

F-Strings: Talking to Python

Before we get to loops, you need to know one small trick that will appear everywhere: f-strings.

An f-string lets you plug a variable directly into a piece of text. You put an f before the quote marks, then wrap any variable in {curly braces}.

python
t = 10

# Without f — Python treats {t} as plain text
print('Spike at {t} ms')   # Spike at {t} ms

# With f — Python replaces {t} with the actual value
print(f'Spike at {t} ms')  # Spike at 10 ms

The f activates the slots. Without it, {t} is just four characters on screen.

Think of it like a Mad Libs template. The f turns on the blanks, and {variable} marks where each value goes.

You can put any variable — or even a calculation — inside the curly braces:

python
voltage = -52.3
neuron_id = 7

print(f'Neuron {neuron_id} voltage: {voltage} mV')
# Neuron 7 voltage: -52.3 mV

print(f'Double the voltage: {voltage * 2} mV')
# Double the voltage: -104.6 mV

You can do math right inside the curly braces. Python evaluates it first, then slots the result in.

F-strings are used everywhere in NMA notebooks — any time a result gets printed, it almost always uses this pattern. Now you'll know exactly what that f means.