Strings in Python String Methods String methods You

  • Slides: 8
Download presentation
Strings in Python String Methods

Strings in Python String Methods

String methods • You do not have to include the string library to use

String methods • You do not have to include the string library to use these! • Since strings are objects, you use the dot notation (like in graphics) • Note that in the methods mentioned which return string results, they create a new string; they do NOT change the old one! • upper(), lower() return a new string with the alphabetic characters in a string changed to the other case, they don’t change any other characters at all • replace(old, new) will return a brand new string with the first pattern, “old”, replaced by the second pattern, “new”, wherever it is in the original string

Strings are immutable • Strings are immutable – meaning they cannot be changed once

Strings are immutable • Strings are immutable – meaning they cannot be changed once they are created • They can be discarded; the variable name can be reassigned to another different string • But individual characters cannot be changed in a string • If name = “Mary” you cannot say name[0]= “S” • To change the first letter of the string you would have to do something like name = ‘S’ + name[1: ] • To make a string all upper case, you would say something like name = name. upper() - that is, upper returns a string and you assign that new string to the same variable, the old string is discarded

Strings and whitespace • whitespace is a specific name for a set of characters,

Strings and whitespace • whitespace is a specific name for a set of characters, namely the blank or space character ‘ ‘, the tab character ‘t’, and the newline character ‘n’ • They are very often used as delimiters (markers between) in various string methods (strip, split) • strip() removes all leading and trailing whitespace characters from a string. It does NOT alter any whitespace characters in the middle of the string. Example: “ abtcdn”. strip() will “abtcd” • str. split() will use whitespace characters as delimiters to break the string into a list of strings (see a following slide)

The find method, the index method, the count method and the in operator •

The find method, the index method, the count method and the in operator • All of these methods and operators involve finding something in a string • They could all be written with loops and if statements but it’s much easier to use prewritten functions • in is the simplest – syntax a in b It returns a Bool if it finds the first string a, somewhere in the string b. It does not matter if there are multiple occurrences of the string a, does not matter where the string a is positioned in the string b. It does have to be an exact match, as far as case, spacing, etc.

The find method and index method • find(target) – this is called with the

The find method and index method • find(target) – this is called with the dot notation on the source string, so a call like s. find(“me”) is looking in the string s for the string “me” • It works from the left end of the string every time • It returns the location of the first occurrence of the target string • It returns -1 if the target string does not occur at all in the source The index method works the same as the find method, except that if the search fails, the index method causes an exception and the program crashes! Safety rule: use the “in” operator first, before using the index method

The count method • The count method is similar to the find and index

The count method • The count method is similar to the find and index methods • count(“m”) will return an integer from 0 up to length of the string • It is a method so it’s called with the dot notation s. count(“m”) would return an integer that shows how many m’s appear in the string

Summary of location/membership • the in operator gives you just True or False, tells

Summary of location/membership • the in operator gives you just True or False, tells you the target string is in the source string or not, NO location • find and index return the location of the left-most occurrence of the target string in the source string (find returns -1 for failure, index crashes the program for failure) • count returns the number of occurrences of the target string in the source string