

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Up until now, we’ve had a small set of programming elements we’ve worked with: Assignment, print, for (with and without range()), if We’re halfway through the class, so we’re going to start pulling back the curtains a little and show what’s behind the scenes Two new concepts: Dot notation for objects Lists
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. 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.
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 if you want phrase = "Monica's cat." print phrase Monica's cat. so that it can contain single quotes Double quotes delimit this string
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""" “Triple” quotes are really triple double quotes!
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 This means we can have up to 256 distinct characters In more recent standards, 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.
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 special characters Backslash escapes “\b” is backspace “\n” is a newline (like pressing the Enter key) “\t” is a tab “\uXXXX” is a Unicode character, where XXXX is a code and each X can be 0-9 or A-F. http://www.unicode.org/charts/ Must precede the string with “u” for Unicode to work Q: If ‘\’ is an escape, how do we print a backslash? A: ‘\’ Getting parts of strings We use the square bracket “[]” notation to get parts of strings. stringVariable[n] gives you the nth^ character in the string (but keep in mind the first one is the “zeroth”) string[n:m] gives you the character indexed by n up to (but not including) the one indexed by m. Getting parts of strings
helloStr = "Hello" print helloStr[1] e print helloStr[0] H print helloStr[2:4] ll H e l l o 0 1 2 3 4
print helloStr Hello print helloStr[:4] Hell print helloStr[3:] lo print helloStr[:] Hello Dot notation (2) All data in Python are actually objects Objects not only store data, but they respond to special functions that only objects of the same type understand. We call these special functions methods Methods are functions known only to certain objects To execute a method, you use dot notation objectName.method()