• No se han encontrado resultados

3.1 Cualidades del software

3.2.1 Metodologías tradicionales y mixtas de desarrollo

You can get data out of a dictionary and include it in your string by using the % operator.

You use a mapping key to stand for the value to be used from the dictionary. Follow these steps to create a line of code that gets the value of the mathematical constant e from a dictionary and prints it as part of a string.

1. Type your string up to the point where you want to include your data, like so:

2. "The value of e is approximately

3. Type % and (in parentheses) the dictionary key for the value you want to

include.

4. "The value of e is approximately %(e)

5. Type the formatting code (so that the whole chunk of code looks something

like this):

6. "The value of e is approximately %(e) f

7. Type the rest of your string, a space, the % operator, a space, and the name of

the dictionary.

8. "The value of e is approximately %(e)f " % mydict

Here's the line of code in context:

>>> math_const = {'pi': 3.141592, 'e': 2.718282}

>>> "The value of e is approximately %(e)f" % math_const 'The value of e is approximately 2.718282'

A formatting example using string methods

You can also use string methods to write code that prints a table of powers, similar to the one in the previous section, "Formatting with multiple data items in a tuple." The

rjust() method right-justifies the string within an area the size of the number of spaces

specified in its argument.

Note that this code has more lines than the code written with the % operator and tuples. If

you're concerned about reducing the size of your programs, the % operator might be the

way to go.

x = 'x' y = 'x**2' z = 'x**x'

print x, y.rjust(4), z.rjust(6) print "=" * 14

for n in range(1,6): nn = str(n**2) nnn = str(n**n)

print n, nn.rjust(4), nnn.rjust(6)

Here's what it prints:

x x**2 x**x ============== 1 1 1 2 4 4 3 9 27 4 16 256 5 25 3125

Unraveling Unicode

So far, you have been using strings that work with English text (and some Western languages, like French and German). But what if you need to use Russian or Japanese? Python 2.0 introduced a special string type called Unicode strings that let you manipulate strings in any language or alphabet.

Unicode strings work mostly the same way as regular strings. Here are a few of their unique features:

• Unicode strings in Python are specified with a lowercase u.

• In most installations of Python, a four-digit hexadecimal number represents each Unicode character.

Creating a Unicode string

To create a Unicode string, precede the string with u, like so:

>>> z = u"Fruit bats pollinate the flowers of many fruits."

To create a Unicode string that uses a non-ASCII Unicode character, follow these steps: 1. Type u to specify a Unicode string and then start typing the string.

2. u'Libert

3. To specify a character that you want to represent by using Unicode, type \u

and the character's hexadecimal value, like so:

4. u'Libert \u00e9

5. Type the rest of the string and the ending quotation mark:

6. u'Libert\u00e9 '

The value 00e9 stands for the é character.

TECHNICAL

STUFF What is Unicode and why do we care about it?

When people started teaching computers how to talk, the computer geeks came up with a system called ASCII (American Standard Code for Information Interchange). ASCII characters are encoded by using 7 bits of information, which allows for only 128 (0–127) characters. That wasn't enough for non-English languages, accented characters, and other common symbols.

Enter Unicode. Unicode will eventually be able to encode every character set, including those for non-European languages, alphabets used by scholars, and mathematical and linguistic symbols. See http://www.unicode.org to find out more.

TECHNICAL

STUFF Location, location, location

How Python stores and displays your Unicode string depends on your locale, which is Python's idea of what language and character set you're using. The locale also determines the default character encoding that Python uses. The encoded string must match that encoding.

Here's how to see your default locale and encoding:

>>> import locale

>>> locale.getdefaultlocale() ['en_US', 'utf']

Locales are tricky because all (repeat, all) character input and output must go through the encode/decode process. When you print a Unicode string, Python automatically tries to do this conversion so that you see the characters you expect. Here's an example of this conversion in action:

>>> print u'Libert\u00e9' Liberté

But you'll see an error if you try printing a Unicode string that contains characters that don't exist in your current locale. For example, if you try to print a Hebrew character with a locale that doesn't support any Hebrew encodings, you'll see this error:

>>> print u'\u05d0' # Hebrew aleph character Traceback (most recent call last):

File "<stdin>", line 1, in <module>

__builtin__.UnicodeEncodeError: 'ascii' codec can't encode character u'\u05d0' in

position 0: ordinal not in range(128)

Note that many modern operating systems support multiple encodings, so you might not see this error if you try it yourself.

A twisty maze of codes

Unicode lets you process text by using only one code. But Unicode can't be used directly with character input and output, so you need to encode output and decode input.

Beginners tend to think of Unicode as another encoding of the character set they are used to using. Thus, they think that they need to encode "regular" text into Unicode and decode Unicode into regular text. That's backward. Here's the correct way to think about it:

When you start using Unicode, whatever regular character set you're using is a part of Unicode. Therefore, you need to encode Unicode characters into your regular character set, and decode your regular character set into Unicode.

Encoding, decoding, and other Unicode methods

All the regular string methods also work with Unicode strings. A couple of methods apply especially to Unicode: encode() and decode().

The list of standard encodings is available at