Book Image

Practical Maya Programming with Python

By : Robert Galanakis
Book Image

Practical Maya Programming with Python

By: Robert Galanakis

Overview of this book

Table of Contents (17 chapters)
Practical Maya Programming with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

String formatting


You will need to know a few things about string formatting to follow along with this book's examples. The topic itself can get very deep, but we will just stick to some basics.

There are two types of string formatting in Python. The older and still popular version uses the percent character (%), while the newer and more flexible version uses the format method on strings. Here is a very basic example of each.

>>> name = 'Jon'
>>> 'Hi, %s!' % name
'Hi, Jon!'
>>> 'Hi, {0}!'.format(name)
'Hi, Jon!'

We use the % version exclusively in this book because it is more concise. I would encourage you to use the format method for new code. Translating this book's examples should be straightforward.

Inside the string being formatted, the % character indicates something that will be replaced, and the characters following the % specify how it will be replaced. For example, consider the difference between %s and %r.

>>> '%s' % 'hi'
'hi'
>>> '%r' % 'hi'
"'hi'"
>>> str('hi')
'hi'
>>> repr('hi')
"'hi'"

The %s sequence converts the argument being formatted using the str function, whereas the %r sequence uses the repr function. We will use %s and %r almost exclusively, but two other common format characters that are good to know about are d and f for integer and floating point representations.

>>> '%d' % 100.0
'100'
>>> '%03d' % 10
'010'
>>> '%f' % 1
'1.000000'
>>> '%.3f' % 1
'1.000'

If the format string has only one positional replacement, the argument can be a tuple with a single value, or a value that is not a tuple. If the format string needs multiple positional replacements, the value should be a tuple with a number of items equal to the number of replacements needed. If the two do not match (there are too few or too many arguments to format), a TypeError will be raised.

>>> 'Hi, %s!' % ('Jon',)
'Hi, Jon!'
>>> '%s, %s!' % ('Hi', 'Jon')
'Hi, Jon!'
>>> '%s, %s!' % 'Hi'
Traceback (most recent call last):
TypeError: not enough arguments for format string
>>> '%s' % ('Hi', 'Jon')
Traceback (most recent call last):
TypeError: not all arguments converted during string formatting

Finally, you can also use keywords for formatting strings. The keyword name goes between the % and format characters, like %(keyword)s. Instead of a tuple you provide a mapping, such as a dictionary, after the % operator. The mapping's keys and values become the format string's keywords and the values being formatted. Extra keys are ignored.

>>> mapping = {'value': 1.2345, 'units': 'km', 'ignore': 1}
>>> '%(value).3f%(units)s' % mapping
'1.234km'

An interesting Pythonic idiom is to use the locals() function as the mapping. The locals() function returns a dictionary where each key and value is a variable and its value.

>>> value = 1.2345
>>> units = 'km'
>>> '%(value).3f%(units)s' % locals()
'1.234km'

There is a lot more to string formatting but this section covers the basics. If you need more in-depth instruction, resources abound on the Internet.