Python's str class A list can represent any sequence of objects A - - PowerPoint PPT Presentation

python s str class
SMART_READER_LITE
LIVE PREVIEW

Python's str class A list can represent any sequence of objects A - - PowerPoint PPT Presentation

Python's str class A list can represent any sequence of objects A very common need in computing is for a sequence of text characters. There is a specialized class, named str , devoted to manipulating character strings.


slide-1
SLIDE 1

Object-Oriented Programming in Python 2-28

Python's str class

  • A list can represent any sequence of objects
  • A very common need in computing is for a

sequence of text characters.

  • There is a specialized class, named str,

devoted to manipulating character strings.

slide-2
SLIDE 2

Object-Oriented Programming in Python 2-29

String literals

  • Can enclose in single quotes: 'bread'
  • Can enclose in double quotes: "bread"
  • This choice helps when you want to use a

single or double quote as a character within the string: "Who's there?"

  • Can embed a newline character using an

escape character \n as in: "Knock Knock\nWho's there?"

slide-3
SLIDE 3

Object-Oriented Programming in Python 2-30

Common behaviors

greeting = 'How do you do?'

  • len(greeting)

returns 14

  • 'yo' in greeting

returns True

  • greeting.count('do')

returns 2

  • greeting.index('do')

returns 4

  • greeting[2]

returns 'w'

slide-4
SLIDE 4

Object-Oriented Programming in Python 2-31

Slicing is a generalization of indexing that is supported by strings (and lists too).

Slicing

1111111111222222 01234567890123456789012345 alphabet = 'abcdefghijklmnopqrstuvwxyz'

slide-5
SLIDE 5

Object-Oriented Programming in Python 2-32

Slicing

1111111111222222 01234567890123456789012345 alphabet = 'abcdefghijklmnopqrstuvwxyz'

Slicing is a generalization of indexing that is supported by strings (and lists too).

alphabet[4] returns 'e'

slide-6
SLIDE 6

Object-Oriented Programming in Python 2-33

Slicing

1111111111222222 01234567890123456789012345 alphabet = 'abcdefghijklmnopqrstuvwxyz'

Slicing is a generalization of indexing that is supported by strings (and lists too).

alphabet[4:13] returns 'efghijklm' (starting at 4, going up to but not including 13)

slide-7
SLIDE 7

Object-Oriented Programming in Python 2-34

Slicing

1111111111222222 01234567890123456789012345 alphabet = 'abcdefghijklmnopqrstuvwxyz'

Slicing is a generalization of indexing that is supported by strings (and lists too).

alphabet[ :6] returns 'abcdef' (starting at beginning going up to but not including 6)

slide-8
SLIDE 8

Object-Oriented Programming in Python 2-35

Slicing

1111111111222222 01234567890123456789012345 alphabet = 'abcdefghijklmnopqrstuvwxyz'

Slicing is a generalization of indexing that is supported by strings (and lists too).

alphabet[23:] returns 'xyz' (starting at 23 going all the way to the end)

slide-9
SLIDE 9

Object-Oriented Programming in Python 2-36

Slicing

1111111111222222 01234567890123456789012345 alphabet = 'abcdefghijklmnopqrstuvwxyz'

Slicing is a generalization of indexing that is supported by strings (and lists too).

alphabet[9:20:3] returns 'jmps' (starting at 9, stopping before 20, stepping by 3)

slide-10
SLIDE 10

Object-Oriented Programming in Python 2-37

Slicing

1111111111222222 01234567890123456789012345 alphabet = 'abcdefghijklmnopqrstuvwxyz'

Slicing is a generalization of indexing that is supported by strings (and lists too).

alphabet[17:5:-3] returns 'roli' (starting at 17, toward but not with 5, stepping by -3)

slide-11
SLIDE 11

Object-Oriented Programming in Python 2-38

Slicing

1111111111222222 01234567890123456789012345 alphabet = 'abcdefghijklmnopqrstuvwxyz'

Slicing is a generalization of indexing that is supported by strings (and lists too).

alphabet[ : :-1] 'zyxwvutsrqponmlkjihgfedcba' (everything, but in reverse order)

slide-12
SLIDE 12

Object-Oriented Programming in Python 2-39

Summary of Slicing

Notice that convention for slicing alphabet[start:stop:step] uses indices akin to that of range(start, stop, step)

slide-13
SLIDE 13

Object-Oriented Programming in Python 2-40

Differences: list and str

  • We cannot change an existing string.
  • However, we can create new strings based

upon existing ones.

  • List are mutable; strings are immutable

(allows Python to optimize the internals)

slide-14
SLIDE 14

Object-Oriented Programming in Python 2-41

Example: lower( )

>>> formal = 'Hello' >>>

str 'Hello' formal

slide-15
SLIDE 15

Object-Oriented Programming in Python 2-42

Example: lower( )

>>> formal = 'Hello' >>> informal = formal.lower() >>>

str 'hello' informal str 'Hello' formal Note that formal is unchanged

slide-16
SLIDE 16

Object-Oriented Programming in Python 2-43

Reassigning an Identifier

>>> person = 'Alice' >>>

str 'Alice' person

slide-17
SLIDE 17

Object-Oriented Programming in Python 2-44

Reassigning an Identifier

>>> person = 'Alice' >>> person = person.lower() >>>

str 'alice' person str 'Alice'

slide-18
SLIDE 18

Object-Oriented Programming in Python 2-45

Creating New Strings

  • greeting.lower( )
  • greeting.upper( )
  • greeting.capitalize( )
  • greeting.strip( )
  • greeting.center(30)
  • greeting.replace('hi','hello')

Each of the following leaves the original string unchanged, returning a new string as a result.

slide-19
SLIDE 19

Object-Oriented Programming in Python 2-46

Additional string methods

  • greeting.islower( )

not to be confused with lower( )

  • greeting.isupper( )
  • greeting.isalpha( )
  • greeting.isdigit( )
  • greeting.startswith(pattern)
  • greeting.endswith(pattern)

Strings support other methods that are specific to the context of textual information

slide-20
SLIDE 20

Object-Oriented Programming in Python 2-47

Converting between strings and lists

  • To support text processing, the str class has

methods to split and rejoin strings.

  • split is used to divide a string into a list of

pieces based upon a given separator.

  • join is used to assemble a list of strings and

a separator into a composite string.

slide-21
SLIDE 21

Object-Oriented Programming in Python 2-48

The split method

By default, the pieces are based on dividing the original around any form of whitespace (e.g., spaces, tabs, newlines)

>>> request = 'eggs and milk and apples' >>> request.split( ) ['eggs', 'and', 'milk', 'and', 'apples']

slide-22
SLIDE 22

Object-Oriented Programming in Python 2-49

The split method

Some other separator can be specified as an

  • ptional parameter to split. That string will be

used verbatim.

>>> request = 'eggs and milk and apples' >>> request.split('and') ['eggs ', ' milk ', ' apples'] ^ ^ ^ ^

(note well the spaces that remain)

slide-23
SLIDE 23

Object-Oriented Programming in Python 2-50

The split method

Here is the same example, but with spaces embedded within the separator string.

>>> request = 'eggs and milk and apples' >>> request.split(' and ') ['eggs', 'milk', 'apples']

slide-24
SLIDE 24

Object-Oriented Programming in Python 2-51

The join method

The join method takes a sequence of strings and combines them using a given string separator between each pair. Formally, this method is invoked upon the separator.

>>> guests = ['John', 'Mary', 'Amy'] >>> conjunction = ' and ' >>> conjunction.join(guests) 'John and Mary and Amy'

slide-25
SLIDE 25

Object-Oriented Programming in Python 2-52

The join method

The separator is often expressed as a literal.

>>> guests = ['John', 'Mary', 'Amy'] >>> ' and '.join(guests) 'John and Mary and Amy'

The sequence could be a string of characters.

>>> '-'.join('respect') 'r-e-s-p-e-c-t'