things to keep

diverting content of a developers life

Python - Basic - print variables inline of a string

2023-01-29 1 min read dev ckroeger

I’m new to python and asked me how to print variables in a string without concatination.

Here it is:

# first we need data
karl = {
  'who': 'karl',
  'days':
     {
        29: 4,
        28: 4,
        27: 6,
        26: 4,
        25: 4
    }
}
print(f"{karl['who']}: {karl['days'][26]}")

Output:

karl: 4

The var-name between the curly braces will be printed.

varData = "world"
print(f"Hello {varData}")

Output:

Hello world