ch10 - Chapter 10: Creating and Modifying Text 1 Chapter...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 10: Creating and Modifying Text 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter Objectives 2
Background image of page 2
Text Text is the universal medium We can convert any other media to a text representation. We can convert between media formats using text. Text is simple. Like sound, text is usually processed in an array a long line of characters We refer to one of these long line of characters as a string. In many (especially older) programming languages, text is actually manipulated as arrays of characters. It’s horrible! Python actually knows how to deal with strings. 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Strings Strings are defined with quote marks. Python actually supports three kinds of quotes: >>> print 'this is a string' this is a string >>> print "this is a string" this is a string >>> print """this is a string""" this is a string Use the right one that allows you to embed quote marks you want >>> aSingleQuote = " ' " >>> print aSingleQuote ' 4
Background image of page 4
Why would you want to use triple quotes? To have long quotations with returns and such inside them. >>> print aLongString() This is a long string >>> def aLongString(): return """This is a long string""" 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Encodings for strings Strings are just arrays of characters In most cases, characters are just single bytes. The ASCII encoding standard maps between single byte values and the corresponding characters More recently, characters are two bytes. Unicode uses two bytes per characters so that there are encodings for glyphs (characters) of other languages Java uses Unicode. The version of Python we are using is based in Java, so our strings are actually using Unicode. 6
Background image of page 6
ASCII encoding through ord() >>> str = "Hello" >>> for char in str: ... print ord(char) ... 72 101 108 108 111 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
There are more characters than we can type Our keyboards don’t have all the characters available to us, and it’s hard to type others into strings. Backspace Return We use backslash escapes to get other characters into strings 8
Background image of page 8