Closure#

Syllabus week 7: Strings#

  • A string is a sequence of characters (each character itself is also a string), revisit sequences, elements, indices

  • String indexing

  • String slicing

  • Negative index

  • Methods and invoking a method

  • String methods: upper(), lower(), find(), index(), count(), strip()

  • Keyword in as a membership operator

  • Revisit the len() function for strings

  • Traversing strings, keyword in for traversal

  • f-strings, and string formatting options

  • Escape sequence, e.g. "/n"

  • String methods with lists as return value or argument: split() and join()

  • The repr() function and using it to display strings with escape sequences visible

Advanced#

Note about the material in Advanced section. The advanced material contains more some additional topics related to the weeks’s content. You are not required to read this material, and none of the exercises or exam questions will rely on this material.

Advanced 7.1: Chaining String Methods#

String methods return a sting, which allows you to chain them. For example, you can chain replace as shown below.

s = "Zinc is a metal".replace("Zinc", "Carbon").replace("metal", "non-metal")
print(s)

This code is equivalent to:

s = "Zinc is a metal"
s = s.replace("Zinc", "Carbon")
s = s.replace("metal", "non-metal")
print(s)

Sometimes the order of the chained methods matters. The leftmost method is applied first, and the rightmost method is applied last. Which order would you apply

  1. my_string.replace("sapiens", "sapiens are human")

  2. my_string.replace("sapiens sapiens", "sapiens")

to convert "homo sapiens sapiens" to "homo sapiens are human"?

Advanced 7.2: Split with Max Splits#

The split method can also take a second argument, which is the maximum number of splits to perform. Here is an example.

sentence = "Python is a great programming language"
max_splits = 2
print(sentence.split(" ", max_splits))
['Python', 'is', 'a great programming language']

Advanced 7.3: Unicode Characters#

In this course we only work with strings consisting of letters from the English alphabet, some common punctuation, and escape characters for new line. However, Python strings may consist of other characters and symbols from the Unicode character set. Check https://home.unicode.org/ and for an example look at the code below.

clapper = "👏"
print('Cool! ' + 3 * clapper)

# Other ways of defining characters
note = "\u2669"
delta = "\N{GREEK CAPITAL LETTER DELTA}"
bee = "\U0001F41D"
smiley = "\U0001F603"
print(note, delta, bee, smiley)
Cool! 👏👏👏
♩ Δ 🐝 😃

Advanced 7.4: More F-strings#

For additional practice with f-strings, print my_float = 1/7 and my_int = 729 to get the following outputs:

0.14285714
0.1
0
0000.14
 729
0729
729.0

Advanced 7.5: Self-documenting F-string#

Printing values of variables is an excellent way to debug. To assist this f-stings have a special syntax with “=” specifier. Run the code below to see how it works.

first_number = 14
second_number = 3
print(f"{first_number=}, {first_number+second_number=}")
first_number=14, first_number+second_number=17

Advanced 7.6: More New Line#

Run the code below to print a multi-line sign.

text_wall = "-" * 27 + "\n" + ("¦ W A L L   O F   T E X T ¦\n" * 10) + "-" * 27
print(text_wall)
---------------------------
¦ W A L L   O F   T E X T ¦
¦ W A L L   O F   T E X T ¦
¦ W A L L   O F   T E X T ¦
¦ W A L L   O F   T E X T ¦
¦ W A L L   O F   T E X T ¦
¦ W A L L   O F   T E X T ¦
¦ W A L L   O F   T E X T ¦
¦ W A L L   O F   T E X T ¦
¦ W A L L   O F   T E X T ¦
¦ W A L L   O F   T E X T ¦
---------------------------

Advanced 7.7: Multi-line Strings#

If you want to define a paragraph of text in Python, you can use a multi-line string. This is done by enclosing the string in triple quotes """ or '''. A escape character for new line \n is going to be added automatically.For example:

lyrics = """When you were here before
Couldn't look you in the eye
You're just like an angel
Your skin makes me cry
You float like a feather
In a beautiful world
I wish I was special"""
print(lyrics)

This can also be useful if you want to make a long comment but adding # to every line is cumbersome. Just writing a (long) string without assigning it to a variable will not have any effect on the program. It is often used for docstrings in functions. A docstring is a description (documentation) of a function that is written (as a string) at the beginning of the function to make it easier to understand what the function does. For example:

def greatest_common_divisor(a,b):
    """
    Finds the greatest common divisor of two numbers.
    
    Parameters:
        a (int): The first number.
        b (int): The second number.

    Returns:
        int: The greatest common divisor of a and b.
    """
    divisor = 1
    for i in range(2, min(a,b)+1):
        if a % i == 0 and b % i == 0:
            divisor = i
    return divisor

Advanced 7.8: Shake It off#

The following are chorus lyrics to the song “Shake it off” by Taylor Swift.

lyrics = """
'Cause the players gonna play,
And the haters gonna hate,
Baby, I'm just gonna shake,
I shake it off, I shake it off (hoo-hoo-hoo)
Heartbreakers gonna break,
And the fakers gonna fake,
Baby, I'm just gonna shake,
I shake it off, I shake it off (hoo-hoo-hoo)
"""

However, each line that does not end in (hoo-hoo-hoo) should have the last word repeated 5 times and separated by commas. For example, the first line should be 'Cause the players gonna play, play, play, play, play. Write a script that prints the corrected lyrics. Note that when defining a multiline string like above, a new line character is added at the end of each line.