A string in Python is a series of characters — letters, numbers, and other symbols like underscore ( _ ) and period ( . ). You can create a string by surrounding a series of characters with either single quotes ( ' ) or double quotes ( " ).
For example, both of the following statements print "Python strings" to the screen.
print 'Python strings'
print "Python strings"
For simple string printing, you can print a mixture of strings and other values by separating them with commas:
red = .7
green = .2
print 'The red value is', red, 'and the green value is', green
For more complex creation of strings from different types of values, Python provides the string formatting operator, % (the percent sign). The formatting operator uses two inputs: a string with formatting characters and the data to be formatted, separated by the percent sign.
For example, the formatting character for integers is d. To insert an integer into a string, you use %d where you want the integer. For example, if you start Python and enter:
number = 23
name = 'Picture number %d' % (number)
print name
then
Picture number 23
will be printed.
This is a lot of work if you already know how the string should look in advance. When we make image sequences, however, we'll need to create filenames using a series of frame numbers. The frame number will be used by the string formatting operator to create the filename.
For example, let's say we want to create a series of filenames from 1 to 5. We can use the string formatting operator to create the filenames. Try entering this in Python:
for frame in range(1,6):
filename = 'mantis.%d.tif' % (frame)
print filename
We could use the %d specifier to make filenames for any frame number we like. However, the typical sorting order for listing files (with the ls command in a Unix shell) will list mantis.10.tif before mantis.2.tif. This is the alphabetical order, but it isn't the order that we'd like to see for our image sequences. The solution is to pad the frame number with zeros. The ls command will then sort the files in the order we expect.
To pad a frame number, we use a modified version of the %d specifier. After the % and before the d, we add:
for frame in range(1,6):
filename = 'mantis.%04d.tif' % (frame)
print filename
Notice that %d is now %04d — use the zero character to pad, and
pad to four characters.
There can be any number of formatting characters in the string, and they refer to the values in the list that follows the percent sign. For example, let's say that we are including a version number in our filenames. This version number will increase each time we generate a new version of our sequence. We can include the version number as part of the filename using the formatting operator:
version = 3
# ...
for frame in range(1,6):
filename = 'mantis_v%02d.%04d.tif' % (version, frame)
print filename
We're assuming that we won't have more than 99 versions of our
sequence so we're using a version number that is padded to two
spaces. If we run the above code, the following is printed to the screen:
mantis_v03.0001.tif
mantis_v03.0002.tif
mantis_v03.0003.tif
mantis_v03.0004.tif
mantis_v03.0005.tif
Try putting the above Python code in a file and then change the padding
character and the padding size. We'll be seeing the percent
formatting character in all the sequence scripts we write.