Today: strings, loops, string functions, Doctests

Week-3 Thoughts

String Reminder

-See guide: Python-String

str int Conversion

Loop Over Every Char in a String?

string 'Python' shown with index numbers 0..5

Loop Over Every Char

# have string s
for i in range(len(s)):
    # use s[i] in here

Example 1. double_char

(these work for everyone now, sorry for the snafu)

> 1.double_char

Solution code

def double_char(s):
    result = ''
    for i in range(len(s)):
        result = result + s[i] + s[i]
    return result

String Boolean Tests - True or False

String Testing == 'a' 'A'

We've used == already. 'a' and 'A' are different characters.

>>> s = 'red'
>>> s == 'red'   # two equal signs
True
>>> s == 'Red'   # must match exactly
False

String Testing - in

>>> 'c' in 'abcd'
True
>>> 'bc' in 'abcd'
True
>>> 'bx' in 'abcd'
False
>>> 'A' in 'abcd'
False

String Character Class Tests

s.isdigit() - True if all chars in s are digits '0' '1' .. '9'

s.isalpha() - True for alphabetic word char, i.e. a-z A-Z. Each unicode alphabet has its own definition of what's alphabetic, e.g. 'Ω' below is alphabetic.

s.isalnum() - alphanumeric, just combines isalpha() and isdigit()

s.isspace() - True for whitespace char, e.g. space, tab, newline

>>> 'a'.isalpha()
True
>>> 'abc'.isalpha()  # works for multiple chars too
True
>>> 'Z'.isalpha()
True
>>> '@'.isalpha()
False
>>> '9'.isdigit()
True
>>> ' '.isspace()
True

Example 2. digits_only

> 2.digits_only

Solution code

def digits_only(s):
    result = ''
    for i in range(len(s)):
        if s[i].isdigit():
            result += s[i]
    return result

if Variation: if / else

if test:
  Lines-A
else:
  Lines-B

Example 3. str_dx

> 3.str_dx

Solution code

def str_dx(s):
    result = ''
    for i in range(len(s)):
        if s[i].isdigit():
            result += 'd'
        else:
            result += 'x'
    return result

Big Picture - Program, Functions, Testing

alt: program made of functions, each with tests

str1 project

Python Function - Pydoc

def str_dx(s):
    """
    Given string s.
    Returns a string where every digit is changed to 'd',
    and all other chars are changed to 'x'.
    (this code + tests are complete)
    >>> str_dx('Hi4!x3')
    'xxdxxd'
    >>> str_dx('123')
    'ddd'
    >>> str_dx('')
    ''
    """

Python Function - Doctest

See lines like:

    >>> str_dx('Hi4!x3')
    'xxdxxd'

String Test: Upper / Lower Case

>>> 'a'.islower()
True
>>> 'A'.islower()
False
>>> 'A'.isupper()
True
>>> '9'.isupper()
False

String Change Upper/Lower Case

s.lower() - returns a new version of s where each char is converted to its lowercase form, so 'A' becomes 'a'. Chars like '$' are unchanged. The original s is unchanged - a good example of strings being immutable. Each unicode alphabet includes its own rules about upper/lower case.

s.upper() - returns an uppercase version of s

>>> s = 'Python123'
>>> s.lower()
'python123'
>>> s.upper()
'PYTHON123'
>>> s
'Python123'

Programming With Doctest

Doctest Example 2, crazy_str()

Write code for this problem. Use the Doctests as you go.

def crazy_str(s):
    """
    Given string s. Return a string where every lowercase char
    in s is converted to uppercase, and every other char
    is converted to lowercase.
    >>> crazy_str('Hello')
    'hELLO'
    >>> crazy_str('abc2@Z')
    'ABC2@z'
    >>> crazy_str('')
    ''
    """
    pass