## Built-in class `str`

### Purpose

• Supports the manipulation of character sequences.
• Strongly suggest you read the epistle on mathematical interval notation before checking out slicing operations.

### Suppose

`a = 'abcdefghijklm'`

`b = 'Abra-Ca-Dabra!!'`

`c = 'ra'`

`d = '314159'`

`e = '3.1415'`

`f = 'cs 1112'`

### Built-in function and operators

`len( s )`

• Returns the length of the sequence `s`.
• Sample usage: `len( a )`

`str( n )`

• Returns string representation of `n`.
• Sample usage: `str( 12345 )`

Returns `'12345'`.

• Sample usage: `str( 12.345 )`

Returns `'12.345'`.

`[ i : j ]`

• Returns a substring (slice) of its string. The substring is the string's characters at indices `i` to index `j-1`.
• Sample usage: `a[ 2 : 6 ]`

Returns `'cdef'`. They are the characters at indices `2` through `5` in `'abcdefghijklm'`.

`[ i : ]`

• Returns the string's characters at from index `i` on
• Sample usage: `a[ 2 : ]`

Returns `'cdefghijklm'`. They are the characters at indices `0` through `5` in `'abcdefghijklm'`.

`[ : j ]`

• Returns the string's characters at indices `0` to `j-1`.
• Sample usage: `a[ : 6 ]`

Returns `'abcdef'`. They are the characters at indices `0` through `5` in `'abcdefghijklm'`.

`[ i ]`

• Returns the character at index `i`.
• Sample usage: `a[ 2 ]`

Returns `'c'`. The character at index `6` in `'abcdefghijklm'`.

`[ : ]`

• Returns the string's characters at indices `0` to `n-1`, where `n` is the length of the string; that is, returns a copy of the string.
• Sample usage: `a[ : ]`

Returns `'abcdefghijklm'`. They are the characters at indices `0` through `5` in `'abcdefghijklm'`.

### Essential member methods

`capitalize()`

• Returns a new copy of its string with first character capitalized and the other characters in lowercase.
• Sample usage: `a.capitalize()`

Returns `'Abcdefghijklm'`.

`count( s )`

• Returns the number of occurrences of `s` in its string.
• Sample usage: `b.count( c )`

Returns 2. There are two occurrences of `'ra'` in `'Abra-Ca-Dabra!!'`.

`count( s, i, j )`

• Returns the number of occurrences of `s` in its string in index interval [`i`, `j`).
• Sample usage: `b.count( c, 4, 12 )`

Returns 1. There is one occurrence of `'ra'` in `'Abra-Ca-Dabra!!'` in index interval [ 4, 12 )

`find( s )`

• Returns the index of the first occurrence of `s` in its string. If there are no occurrences, returns `-1`.
• Sample usage: `a.find( c )`

Returns `-1`. There are no occurrences of `'ra'` in `'abcdefghijklm'`.

• Sample usage: `b.find( c )`

Returns `2`. First occurrence of `'ra'` in `'abcdefghijklm'` is at index `2`.

`find( s, i )`

• Returns the index of the first occurrence of `s` given `s` is in string slice `[ i : n ]`, where `n` is the length of the string. If there are no occurrences, returns `-1`.
• Sample usage: `a.find( c, 3 )`

Returns `-1`. There are no occurrences of `'ra'` in `'abcdefghijklm'`.

• Sample usage: `b.find( c, 3 )`

Returns `11`. First occurrence of `'ra'` in `'Abra-Ca-Dabra!!'` after index `3` is at index `11`.

`find( s, i, j )`

• Returns the first occurrence of `s` given `s` is in string slice `[ i : j ]`. If there are no occurrences, returns `-1`.
• Sample usage: `b.find( c, 4, 14 )`

Returns 11. The first occurrence of `'ra'` in `'Abra-Ca-Dabra!!'` within the indicated section is at index `11`.

`format( v1, v2, ... )`

• Returns a formatted version of the `v` values. See the writeup about field formatting.

`isalpha()`

• If its string is empty, it returns `False`; Otherwise, returns whether all characters in its string are alphabetic.
• Sample usage: `a.isalpha()`

Returns `True` as all characters in `'abcdefghijklm'` are alphabetic.

• Sample usage: `b.isalpha()`

Returns `False` as not all characters in `'Abra-Ca-Dabra!!'` are alphabetic.

`isdigit()`

• If its string is empty, it returns `False`; Otherwise, returns whether all characters in its string are base 10 digits.
• Sample usage: `d.isdigit()`

Returns `True` as all characters in `'314159'` are digits.

• Sample usage: `e.isdigit()`

Returns `False` not all characters in `'3.1415'` are digits.

`lower()`

• Returns a new copy of its string with all characters converted to lowercase
• Sample usage: `b.lower()`

Returns `abra-ca-dabra!!`.

`replace( s, t, n )`

• Parameter `n` is optional. If `n` is not there, it returns a new copy of its string with all occurrences of `s` replaced by `t`. If `n` is given, it returns a new copy with the first `n` occurrences of `s` replaced by `t`.
• Sample usage: `r.replace( 'a', '___' )`

Returns `'Abr___-C___-D___br___!!'`. All occurrences of `'a'` in `'Abra-Ca-Dabra!!'` are replaced with `'___'`. Note, `'A'` is not changed.

• Sample usage: `b.replace( 'a', '___', 2 )`

Returns `'Abr___-C___-Dabra!!'`. The first two occurrences of `'a'` in `'Abra-Ca-Dabra!!'` are replaced with `'___'`.

`rfind( s )`

• Returns the index of the last occurrence of `s` in its string. If there are no occurrences, returns `-1`.
• Sample usage: `b.rfind( c )`

Returns `11`. Last occurrence of `'ra'` in `'Abra-Ca-Dabra!!'` is at index `11`.

`rfind( s, i, j )`

• Returns the last occurrence of `s` in its string in index interval [`i`, `j`). If there are no occurrences, returns `-1`.
• Sample usage: `b.rfind( c, 1, 10 )`

Returns 2. The last occurrence of `'ra'` in `'Abra-Ca-Dabra!!'` in index interval [ 1, 14 ) is `2`.

`split()`

• Returns a list of words in its string. See the next method listed here when an optional parameter is given.
• Sample usage: `f.split()`

Returns the list `[ 'cs', '1112' ]`.

`split( d )`

• Returns a list of parts in its string. Removes any occurances of the parameter `d` to determine how many parts to create
• Sample usage: `b.split( 'a' )`

Returns the list `[ 'Abr', '-C', '-D', 'br', '!!' ]`.

`strip()`

• Returns a new string without any leading or trailing white-space (tabs, spaces, or newlines). If given a parameter, removes those characters instead.
• Sample usage: `' cs 1112 '.strip()`

Returns the string `'cs 1112'``.

• Sample usage: `a.strip( 'bam' )`

Returns the string `'cdefghijkl'``.

`upper()`

• Returns a new copy of its string with all characters converted to uppercase.
• Sample usage: `b.upper()`

Returns `ABRA-CA-DABRA!!`.