python s str class
play

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.


  1. 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. Object-Oriented Programming in Python 2-28

  2. 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?" Object-Oriented Programming in Python 2-29

  3. 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' Object-Oriented Programming in Python 2-30

  4. Slicing Slicing is a generalization of indexing that is supported by strings (and lists too). 1111111111222222 01234567890123456789012345 alphabet = ' abcdefghijklmnopqrstuvwxyz ' Object-Oriented Programming in Python 2-31

  5. Slicing Slicing is a generalization of indexing that is supported by strings (and lists too). 1111111111222222 01234567890123456789012345 alphabet = ' abcdefghijklmnopqrstuvwxyz ' alphabet[4] returns ' e ' Object-Oriented Programming in Python 2-32

  6. Slicing Slicing is a generalization of indexing that is supported by strings (and lists too). 1111111111222222 01234567890123456789012345 alphabet = ' abcdefghijklmnopqrstuvwxyz ' alphabet[4:13] returns ' efghijklm ' (starting at 4, going up to but not including 13) Object-Oriented Programming in Python 2-33

  7. Slicing Slicing is a generalization of indexing that is supported by strings (and lists too). 1111111111222222 01234567890123456789012345 alphabet = ' abcdefghijklmnopqrstuvwxyz ' alphabet[ :6] returns ' abcdef ' (starting at beginning going up to but not including 6) Object-Oriented Programming in Python 2-34

  8. Slicing Slicing is a generalization of indexing that is supported by strings (and lists too). 1111111111222222 01234567890123456789012345 alphabet = ' abcdefghijklmnopqrstuvwxyz ' alphabet[23:] returns ' xyz ' (starting at 23 going all the way to the end) Object-Oriented Programming in Python 2-35

  9. Slicing Slicing is a generalization of indexing that is supported by strings (and lists too). 1111111111222222 01234567890123456789012345 alphabet = ' abcdefghijklmnopqrstuvwxyz ' alphabet[9:20:3] returns ' jmps ' (starting at 9, stopping before 20, stepping by 3) Object-Oriented Programming in Python 2-36

  10. Slicing Slicing is a generalization of indexing that is supported by strings (and lists too). 1111111111222222 01234567890123456789012345 alphabet = ' abcdefghijklmnopqrstuvwxyz ' alphabet[17:5:-3] returns ' roli ' (starting at 17, toward but not with 5, stepping by -3) Object-Oriented Programming in Python 2-37

  11. Slicing Slicing is a generalization of indexing that is supported by strings (and lists too). 1111111111222222 01234567890123456789012345 alphabet = ' abcdefghijklmnopqrstuvwxyz ' alphabet[ : :-1] ' zyxwvutsrqponmlkjihgfedcba ' (everything, but in reverse order) Object-Oriented Programming in Python 2-38

  12. Summary of Slicing Notice that convention for slicing alphabet[start:stop:step] uses indices akin to that of range(start, stop, step) Object-Oriented Programming in Python 2-39

  13. Differences: list and str • List are mutable ; strings are immutable (allows Python to optimize the internals) • We cannot change an existing string. • However, we can create new strings based upon existing ones. Object-Oriented Programming in Python 2-40

  14. Example: lower( ) >>> formal = 'Hello' >>> formal str 'Hello' Object-Oriented Programming in Python 2-41

  15. Example: lower( ) >>> formal = 'Hello' >>> informal = formal.lower() >>> formal informal str str 'Hello' 'hello' Note that formal is unchanged Object-Oriented Programming in Python 2-42

  16. Reassigning an Identifier >>> person = 'Alice' >>> person str 'Alice' Object-Oriented Programming in Python 2-43

  17. Reassigning an Identifier >>> person = 'Alice' >>> person = person.lower() >>> person str str 'Alice' 'alice' Object-Oriented Programming in Python 2-44

  18. Creating New Strings Each of the following leaves the original string unchanged, returning a new string as a result. • greeting.lower( ) • greeting.upper( ) • greeting.capitalize( ) • greeting.strip( ) • greeting.center(30) • greeting.replace('hi','hello') Object-Oriented Programming in Python 2-45

  19. Additional string methods Strings support other methods that are specific to the context of textual information • greeting.islower( ) not to be confused with lower( ) • greeting.isupper( ) • greeting.isalpha( ) • greeting.isdigit( ) • greeting.startswith(pattern) • greeting.endswith(pattern) Object-Oriented Programming in Python 2-46

  20. 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. Object-Oriented Programming in Python 2-47

  21. 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'] Object-Oriented Programming in Python 2-48

  22. The split method Some other separator can be specified as an optional 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) Object-Oriented Programming in Python 2-49

  23. 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'] Object-Oriented Programming in Python 2-50

  24. 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' Object-Oriented Programming in Python 2-51

  25. 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' Object-Oriented Programming in Python 2-52

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend