HP Caliper Advisor Rule Writer Guide
See the Python Reference Manual for other statements.
Python Strings
Python has a lot of support for creating and manipulating character strings. This is
helpful for generating advice.
Following is some basic information about Python strings:
• String literals are enclosed in single quotes (‘…’), double quotes (“…”), or triple
quotes (“””…”””). Triple quotes are used for multiline text. String literals can
include C-style escape characters using the backslash character. For example:
“text line\n”
• Strings can be explicitly concatenated using the + operator or implicitly by having
strings adjacent to each other (“string 1” ‘ string 2’).
• Strings can be created according to C-like format strings (similar to sprintf()).
The syntax is:
“format_string” % (argument_list)
For example:
“The sum of %d items is: %9.1f” % (count, sum)
“The functions %s and %s have the most dcache misses.” % (name1, name2)
Python Built-In String Operations
Table A-1 “Python Built-In String Operations” shows the available built-in string
operations.
Original strings are not changed. For functions returning a string, it is a new string.
There are more (optional) arguments and functions available. See the Python Reference
Manual for a complete description.
Python also has a library module (“re”) with full regular expression processing.
Table A-1 Python Built-In String Operations
DescriptionReturnsFunction
length of sintegerlen(s)
extract ith character of sstrings[i]
extract slice from ith up to but not including jth character
of s
strings[i:j]
center string s in width positionsstrings.center(width)
left-justify string s in width positionsstrings.ljust(width)
right-justify string s in width positionsstrings.rjust(width)
find first substring sub in string sintegers.find(sub)
find last substring sub in string sintegers.rfind(sub)
Python Strings 69