Simple Python Expression

Hello all,

I was wondering if someone could help me to understand why one of these python expressions works, and the other doesn’t. I am very new to python.

So I’m trying to use the expression to set the file parameter for a moviefilein TOP.

This works:

steve = op(‘moviefilein1’)
dave = “01”
steve.par.file = “Image/” + dave + “.jpg”

This doesn’t:

steve = op(‘moviefilein1’)
dave = str(01)
steve.par.file = “Image/” + dave + “.jpg”

Nor does this:

steve = op(‘moviefilein1’)
dave = 01
steve.par.file = “Image/” + str(dave) + “.jpg”

In each case the error report points to the second digit I have assigned to dave:

SyntaxError: invalid token
Results of run operation resulted in exception.
python >>>
Traceback (most recent call last):
File “</project1/text1:op(’/project1/text1’).run()>”, line 1
td.Error: File “/project1/text1”, line 2
dave = 01
^

str() converts other valid Python objects to a string object
01 is not a valid data type. You’ll see Python code already failing when you try the code dave = 01

01 is not a string otherwise it would have been "01"
01 is not an int, otherwise it would have been 1

If you goal is to add leading zeros to your number in the filepath string, a more Pythonic way would be to do it like either of these:

version one is using zfill to pad your string with zeros

mynumber = 1
filepath = "Image/" + str(mynumber).zfill(2) + ".jpg"
print(filepath)

This will output:
Image/01.jpg

version 2 is using fstrings to format your string:

mynumber = 1
filepath = "Image/" + f"{mynumber:02d}" + ".jpg"
print(filepath)

This will output:
Image/01.jpg

Ahh, I see, that helps a lot, thanks very much.