05.Strings and Sequences

Embed Size (px)

Citation preview

  • 7/30/2019 05.Strings and Sequences

    1/27

    CS 112

    Intro to Programming

    Strings

    Prof. SnyderFall 2011

    George Mason University

  • 7/30/2019 05.Strings and Sequences

    2/27

    String Data Structure

    2

    The String data structure is:

    A complex data type (i.e., it is comprised of

    sub-components or elements)

    A sequence (i.e., a complex data structure

    that consists of sequential elements, which

    implies a specific ordering.

    Immutable (i.e., once created, cannot be

    modified)

  • 7/30/2019 05.Strings and Sequences

    3/27

    Complex Data Type

    3

    Non-complex or primitive data types consist of a

    singular item of data:

    integer, float, boolean, etc.

    Complex data types (structures) comprise zeroor more sub elements:

    Strings comprise zero (e.g., an empty string)

    or more characters.

  • 7/30/2019 05.Strings and Sequences

    4/27

    Sequence Data Types

    4

    Sequential data types comprise sub-elements

    that possess a particular ordering:

    one_string = ABC

    another_string = ABC

    These represent two distinct, unique string data.

  • 7/30/2019 05.Strings and Sequences

    5/27

    Immutable Data Types

    5

    Immutable data types may not be modified once

    they are declared:

    They may, however, be re-declared:

    Attempting to change an element of an immutable data type

    Re-declaration of a_string

  • 7/30/2019 05.Strings and Sequences

    6/27

    Sequence Operations

    6

    (all sequences, not just strings)

  • 7/30/2019 05.Strings and Sequences

    7/27

    Membership

    7

  • 7/30/2019 05.Strings and Sequences

    8/27

    Repetition

    8

  • 7/30/2019 05.Strings and Sequences

    9/27

    Indexing

    9

  • 7/30/2019 05.Strings and Sequences

    10/27

    Indexing

    10

    Accessing the 0th indexed element of

    a_string

    Accessing the (negative) 1st element of

    a_string

  • 7/30/2019 05.Strings and Sequences

    11/27

    Indexing (Loops)

    11

    The variable i takes

    on the value of each

    successive elementof the sequence

    during each

    successive iteration

    of the loop

    Limitation: can only iterate through

    the sequence elements in order

  • 7/30/2019 05.Strings and Sequences

    12/27

    Length

    12

    Number of elements in the

    sequence

  • 7/30/2019 05.Strings and Sequences

    13/27

    Indexing (Loops)

    13

    The variable i takes on

    the value of eachsuccessive element of

    the range during each

    successive iteration of

    the loop

  • 7/30/2019 05.Strings and Sequences

    14/27

    Indexing (Loops)

    14

    The variable i takes onthe value of each

    successive element of the

    sequence during each

    successive iteration of the

    loop

    More robust: can iterate through the

    sequence elements in any manner

  • 7/30/2019 05.Strings and Sequences

    15/27

    Slicing

    15

  • 7/30/2019 05.Strings and Sequences

    16/27

    Slicing

    16

    Strange...

  • 7/30/2019 05.Strings and Sequences

    17/27

    Practice ProblemUsing indexes and slices, what are these expressions

    values?

    word = "dysfunctional" group = "team"

    word[1]

    word[3:6]word[3:]

    word[4:11]

    word[len(word)-1]

    group[-1:-4:-2]

    group[-1:-4:-2]+group[-2::-2]

    group[:3]

    group [-len(group)]

    word[-6:-3:2]+group[:2]+group[3]

    'y'

    'fun'

    'functional'

    'unction'

    'l'

    'me'

    'meat''tea'

    't'

    'totem'

  • 7/30/2019 05.Strings and Sequences

    18/27

    Min / Max

    18

    Why?

  • 7/30/2019 05.Strings and Sequences

    19/27

    ord(char): converts single character to

    corresponding ASCII integer value

    chr(int): converts integer value tocorresponding character symbol

    Based on ASCII code value

    American Standard Code for Information Interchange

    7 binary bits 128 unique symbols

    Python also supports Unicode (16-32 bits)

    chr & ord Built-In Functions

    19

  • 7/30/2019 05.Strings and Sequences

    20/27

    ASCII Table

    20

  • 7/30/2019 05.Strings and Sequences

    21/27

    Min / Max

    21

  • 7/30/2019 05.Strings and Sequences

    22/27

    String Methods

    22

    Method: an action or process (defined relative toa particular object) to be performed

    Dot Operator: a dot (period) that is used toaccess an associated variable or call an

    associated function

    object_name.method_name

    strVar = Test

    strVar.upper() would produce TEST

    Refer to Library

    Reference: 5.6.1

  • 7/30/2019 05.Strings and Sequences

    23/27

    String Operations vs.

    Methods

    23

    sequence operations:

    may be built-in functions

    operate on various data types

    example: len len(strVar)

    methods: operate on a single data type

    string object methods (e.g., capitalize)

    strVar.capitalize()

  • 7/30/2019 05.Strings and Sequences

    24/27

    String Formatting

    24

    def main():

    x = input("Enter number: )

    print "counter =", x

    y,z = "counter = ", str(x); print y + z

    print "counter = " + str(x)

    y = "counter = " + str(x); print y

    print "counter = %d" % (x)

    y = "counter"; print "%s = %d" % (y,x)

    z = "%s = %d" % (y,x); print z

    w = " = "; z = "%s%s%d" % (y,w,x);print z

    main()

    Enter number: 3

    counter = 3

    counter = 3counter = 3

    counter = 3

    counter = 3

    counter = 3counter = 3

    counter = 3

  • 7/30/2019 05.Strings and Sequences

    25/27

    Tuples(a quick introduction)

    A tuple is a grouping of values, with a fixedlength.

    Tuples are sequences (ordered).

    Tuples are immutable (can't be changed, butcan be used to construct a new value).

    Syntax: (comma,separated,values)BNF: tuple ::= '(' [expr [, expr]*] ')'

    Examples:(5,1,3,6) ("Mark",True,100) (x+2,z-y)

    () (1,2,3,4,5,6,7,8) (range(10),"hello")

  • 7/30/2019 05.Strings and Sequences

    26/27

    String Formatting

    26

    (Characters that may follow a % for special meaning)

  • 7/30/2019 05.Strings and Sequences

    27/27

    Practice Problem

    What is the resulting string? Are there any errors?

    "I have %d apples." % 15

    "%d / %f = %f" % (7, 3.0, 7/3.0)x = "%d %s"; x % (3, "amigos")

    "%%d prints integers." % ()

    "I %s MadLibs" % (love)

    "%f %d %s" % (4,4.6,"""yo""")"%s %x" % (hex(200),200)

    "%d centimeters is %d inches."

    'I have 15 apples.'

    '7 / 3.000000 = 2.333333''3 amigos'

    '%d prints integers.'

    (love is not defined)

    '4.000000 4 yo''0xc8 c8'

    '%d centimeters is %d inches'