Gaddis Python 6e Chapter 08
Chapter 8: More About Strings
1. Basic String Operations
Many programs perform operations on strings, and Python provides tools for examining and manipulating these strings.
Strings are treated as sequences, sharing similarities with lists and tuples.
2. Accessing Individual Characters in a String
Methods of Accessing Characters:
For Loop Method:
Syntax:
for character in string:Useful for counting occurrences of specific characters.
Indexing Method:
Syntax:
character = my_string[i]Example: Accessing characters in 'Juliet' using their index.
Index Error:
Occurs when accessing an index out of range. Use
len(string)to get the length to avoid this error.
3. String Concatenation
Definition:
Concatenation means appending one string to another.
Methods of Concatenation:
Use
+operator or+=for augmented assignment.Note: The left operand of
+=must be an existing variable to avoid exceptions.
4. Strings Are Immutable
Strings cannot be changed after creation.
Concatenation creates a new string; the original remains unchanged.
Example: Attempting to assign
string[index] = new_characterwill raise an exception.
5. String Slicing
Definition:
Slicing allows obtaining a substring from a string.
Syntax:
substring = string[start:end]returns characters from start to end - 1.
Default Values:
If
startis omitted, it defaults to 0.If
endis omitted, it defaults tolen(string).
Supports step values and negative indexing (from the string's end).
6. Testing and Searching Strings
Use the
inandnot inoperators to check for substrings.Syntax:
substring in stringchecks for presence and returns a Boolean value.
7. String Methods
Many methods exist for string operations:
General format:
mystring.method(arguments).Boolean Checking Methods:
isalnum(): True if string has only letters or digits.isalpha(): True if string has only letters.isdigit(): True if string has only digits.islower(),isupper(),isspace(): Check character cases and spaces.
8. String Modification Methods
Methods that appear to modify strings:
lower(): Converts string to lowercase.upper(): Converts string to uppercase.strip(),lstrip(),rstrip(): Remove whitespaces from respective ends of the string.
9. Searching Substrings
Methods to search substrings include:
find(substring): Returns the lowest index of substring or -1 if not found.replace(old, new): Returns a copy with all instances of 'old' replaced by 'new'.endswith(substring)andstartswith(substring): Check for starting or ending characters.
10. Repetition Operator
Definition:
The
*operator creates multiple copies of a string.
Syntax:
string_to_copy * ngenerates a new string with n copies of string_to_copy.
11. Splitting a String
split()method returns a list of words in a string.By default, it splits by spaces; can specify a different separator as an argument.
Example: 'One two three four' splits to ['One', 'two', 'three', 'four'].
12. String Tokens
Tokens are substrings separated by a delimiter.
Example: 'peach raspberry strawberry vanilla' (space as delimiter).
Tokenizing uses
split()for individual storage.
13. Summary
Chapter covered: String operations including iterating, concatenation, immutability, slicing, testing, string methods, and splitting.