Basic String Operations • Many types of programs perform operations on strings • In Python, many tools for examining and manipulating strings • Strings are sequences, so many of the tools that work with sequences work with strings
Accessing the Individual Characters in a String (cont’d.) • IndexError exception will occur if: • You try to use an index that is out of range for the string Likely to happen when loop iterates beyond the end of the string • len(string) function can be used to obtain the length of a string Useful to prevent loops from iterating beyond the end of a string
String Concatenation • Concatenation: appending one string to the end of another string • Use the + operator to produce a string that is a combination of its operands • The augmented assignment operator += can also be used to concatenate strings • The operand on the left side of the += operator must be an existing variable; otherwise, an exception is raised
Strings Are Immutable • Strings are immutable • Once they are created, they cannot be changed • Concatenation doesn’t actually change the existing string, but rather creates a new string and assigns the new string to the previously used variable • Cannot use an expression of the form • string[index] = new_character • Statement of this type will raise an exception
String Methods • Strings in Python have many types of methods, divided into different types of operations • General format: mystring.method(arguments) • Some methods test a string for specific characteristics • Generally Boolean methods, that return True if a condition exists, and False otherwise
String Methods (cont’d.) • Programs commonly need to search for substrings • Several methods to accomplish this: • endswith(substring): checks if the string ends with substring • Returns True or False • startswith(substring): checks if the string starts with substring • Returns True or False
String Methods (cont’d.) • Several methods to accomplish this (cont’d): • find(substring): searches for substring within the string • Returns lowest index of the substring, or if the substring is not contained in the string, returns -1 • replace(substring, new_string): • Returns a copy of the string where every occurrence of substring is replaced with new_string
The Repetition Operator • Repetition operator: makes multiple copies of a string and joins them together • The * symbol is a repetition operator when applied to a string and an integer • String is left operand; number is right • General format: string_to_copy * n • Variable references a new string which contains multiple copies of the original string
Splitting a String • split method: returns a list containing the words in the string • By default, uses space as separator • Can specify a different separator by passing it as an argument to the split method
Fundamental Numerical Methods and Data Analysis 1st Edition by George Collins ISBN 9783110936001 3110936003 - Instantly access the complete ebook with just one click
Fundamental Numerical Methods and Data Analysis 1st Edition by George Collins ISBN 9783110936001 3110936003 - Instantly access the complete ebook with just one click