String Interpolation in Python
This is covered in the text book on page 178.
For many things done in class so far, I've used string interpolation. The syntax that Python uses is borrowed from C's printf function. Because the original printf is so pervasive in computing, many languages have replicated that style of string interpolation in their language.
In my opinion, C still does it best.
Java's System.out.printf is very good, but it doesn't support all of the conversion symbols that C does.
Perl, Ruby, and PHP all have good printf functions.
Python has string interpolation built into the language itself, which means you don't need the print statement to use it.
Conversion Symbols and Syntax
| s | String conversion using str(). This is slow, but works for most needs. |
| d | Decimal number. |
| u | Unsigned decimal number. |
| f | Floating point number. |
| x | Lowercase hexadecimal number |
| X | Uppercase hexadecimal number |
| e | Lowercase exponential number |
| E | Uppercase exponential number |
| % | Percent Sign |
Integer
| % | - | + | 0 | number | .number | (variable) | d |
- - Left justifies the printing (optional)
- + prints positive numbers with (optional)
- 0 Zero pads integer (optional)
- number Represents the total width of the space needed to print value (optional)
- .number Represents the total width of the digit places needed to print value (optional) Note: using the above 0 option will override whatever value used here.
- (label) Replace with a variable name (optional)
- d Signals that this is an integer.
Floating point number
| % | - | + | 0 | number | .number | (variable) | f |
- - Left justifies the printing (optional)
- + prints positive numbers with (optional)
- 0 Zero pads integer part(optional)
- number Represents the total width of the space needed to print value (optional)
- .number Represents the spaces needed in the decimal part and zero pads out to that many places (optional)
- (label) Replace with a variable name (optional)
- f Signals that this is an integer.
Examples
Using an integer.
>>> b = 4
>>> b
4
>>> "%d" % (b)
'4'
>>> "%5d" % (b)
' 4'
>>> "%05d" % (b)
'00004'
>>> "%+05d" % (b)
'+0004'
>>> "%5.2d" % (b)
' 04'
>>> "%5.3d" % (b)
' 004'
>>> "%05.2d" % (b)
'00004'
>>> "%+05.2d" % (b)
'+0004'
>>>
Using a floating point number.
>>> a = 2.13
>>> a
2.1299999999999999
>>> "%.3f" % (a)
'2.130'
>>> "%10.3f" % (a)
' 2.130'
>>> "%010.3f" % (a)
'000002.130'
>>> "%+010.3f" % (a)
'+00002.130'
>>> "%0+10.3f" % (a)
'+00002.130'
>>> "%+010.3f" % (a)
'+00002.130'
>>> "%-+010.3f" % (a)
'+2.130 '
>>>
Using labels at variable replacement poits within strings
If multiple replacement points are used in a single string, they each must be labeled or trail the string in order. String interpolation points can be labeled using "%(label)(format code)". The list that follows the % needs to be a dictionary style list of key/value pairs. Because it is a dictionary style list, it must begin and end with "{" and "}" rather than "(" and ")". The argument list can be changed out with different dictionaries.
>>> badjoke = "There are %(animal)s on a %(vehicle)s."
>>>
>>>>>> badjoke % {'animal': 'pink elephants', 'vehicle': 'parade float'}
'There are pink elephants on a parade float.'
>>>
>>>
>>> samjackson = {}
>>> samjackson['animal'] = 'snakes'
>>> samjackson['vehicle'] = 'plane'
>>>
>>> badjoke % samjackson
'There are snakes on a plane.'
>>>
>>>
>>> jameschurch = {}
>>> jameschurch['animal'] = 'aardvarks'
>>> jameschurch['vehicle'] = 'gondola'
>>>
>>> badjoke % jameschurch
'There are aardvarks on a gondola.'
>>>
Python CGI
Page 855
To use Python in CGI scripts, place a script in the cgi-bin directory under the public_html directory. Make the script executable by running "chmod 755 scriptname.py". Your script should then be able to run.
To retrieve the input variables from an HTML form, you'll need to take them out a script using the cgi.FieldStroage() method. I made a dictionary of input values and defaults, and then attempted to reread those input names from the cgi.FieldStorage() input my inputs dictionary. If a read is successful, the inputs dictionary will be updated. If not, the dictionary default will remain unchanged.
In Python CGI, all print statements will be redirected to the web browser. The first line printed should be "Content-type: text/html\r\n". Everything that follows should be valid HTML.
| hello.py |
#!/usr/bin/env python
import cgi
form = cgi.FieldStorage()
inputs = {'name':'Mr. Nobody'}
for i in inputs:
try:
inputs[i] = form[i].value
except KeyError:
pass
print "Content-type: text/html\r\n"
print """<html>
<head><title>Hello!</title></head>
<body>
CGI Test. Hello, %(name)s!
</body>
</html>""" % inputs
|
Comments (0)
You don't have permission to comment on this page.