12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

I said this was hackish because <strong>Python</strong> really does make a string out of the lines ofcode disabled this way, but this is probably not significant in terms of performance.For large sections of code, it’s also easier than manually adding hash marks beforeeach line and later removing them. This is especially true if you are using a texteditor that does not have support for editing <strong>Python</strong> code specifically. In <strong>Python</strong>,practicality often beats aesthetics.Unicode Strings Encode Larger Character SetsThe last way to write strings in your scripts is perhaps the most specialized, and it’srarely observed outside of web and XML processing. Unicode strings are sometimescalled “wide” character strings. Because each character may be represented withmore than one byte in memory, Unicode strings allow programs to encode richercharacter sets than standard strings.Unicode strings are typically used to support internationalization of applications(sometimes referred to as “i18n,” to compress the 18 characters between the first andlast characters of the term). For instance, they allow programmers to directly supportEuropean or Asian character sets in <strong>Python</strong> scripts. Because such character setshave more characters than can be represented by single bytes, Unicode is normallyused to process these forms of text.In <strong>Python</strong>, you can code Unicode strings in your scripts by adding the letter U(lower- or uppercase) just before the opening quote:>>> u'spam'u'spam'Technically, this syntax generates a Unicode string object, which is a different datatype from the normal string type. However, <strong>Python</strong> allows you to freely mix Unicodeand normal strings in expressions, and converts up to Unicode for mixed-typeresults (more on + concatenation in the next section):>>> 'ni' + u'spam' # Mixed string typesu'nispam'In fact, Unicode strings are defined to support all the usual string-processing operationsyou’ll meet in the next section, so the difference in types is often trivial to yourcode. Like normal strings, Unicode strings may be concatenated, indexed, sliced,matched with the re module, and so on, and cannot be changed in-place.If you ever need to convert between the two types explicitly, you can use the built-instr and unicode functions:>>> str(u'spam') # Unicode to normal'spam'>>> unicode('spam') # Normal to Unicodeu'spam'130 | Chapter 7: Strings

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!