SmithLogo

CSC 111

Introduction to Computer Science Through Programming

Smith Computer Science



Lecture Notes 13: More Strings...and practice!





Recap: Slicing

Basics of slicing:

Say we have this:
my_str = 'hello world!' 


Then some slicing options are:




String Methods

As we mentioned before, Strings are Objects, and in Python, objects have values inside them as well as member functions we call "Methods".

We can access these by using the dot operator, as in:
< my_str > . < method name > (< method arguments > )

One example we've been using a lot is format, as in:

my_str = "duckrabbit"
str_rabb = my_str[4:]
out_str = "a substring of {:s} is {:s}"
print(out_str.format(my_str, str_rabb) )


Here, we're accessing the format member method from the string variable out_str and passing in the arguments my_str, and str_rabb

Try it out here.

The following are some useful String methods.
Remember that you can read more about how to use them in the Python String API

replace

You might want to edit the contents of a string by "replacing" fragments with a known pattern for another pattern.

However, since strings, once created, cannot be edited, what happens is that a new string is created which can be used to overwrite the old one.

Example:
phrase = "time consumes everything but it is also the best"
phrase = phrase.replace("time", "Yak")
phrase = phrase.replace("but", "and")
phrase = phrase.replace("best", "beast")
print(phrase)


The notation my_str.replace(<old>, < new >, < count >) only replaces the first < count > instances of the matched pattern.

find

The following are variations of the use of the find method: find(x) -- Returns the index of the first occurrence of item x in the string, else returns -1. x may be a string variable or string literal.
find(x, start) -- Same as find(x), but begins the search at index start.
find(x, start, end) -- Same as find(x, start), but stops the search at index end - 1:
rfind(x) -- Same as find(x) but searches the string in reverse, returning the last occurrence in the string

Examples:
print("abracadabra".find('abra'))  # Returns 0
print("abracadabra".find('dab'))  # Returns 6
print("abracadabra".find('yak'))  # Returns -1
print("abracadabra".find('ra')) # Returns 2
print("abracadabra".find('ra', 3)) # (after 3) Returns 9
print("abracadabra".find('ra', 3,7)) # (in 3,7) Returns -1
print("abracadabra".rfind('ra'))  # (reverse) Returns 9


count

The method count(x) returns the number of times x occurs in the string:
phrase = "The future ain't what it used to be."
# next line prints 8
print("Number of words in '{:s}' is {:d}".format(phrase,phrase.count(" ")+1) )


Questions about the String's format

You can use some methods to ask questions about the string:

New strings with modifications

You can use create new strings with the following methods:

For example, one creates a new string when capitalizing a base one:

my_str = "asap"
new_str = my_str.upper()
print(new_str)


Chaining method calls

sequences of calls can be done on each resulting string. Since one call results in a string, the next call can be simply appended to the previous one.

Activity 4 [2 minutes]:
What does the following program print?

my_str = "   nEVer tHe leSs    "
print ( my_str.strip().replace(" ", "").title() )




Splitting and Joining Strings

Another typical task is to separate a string into a list of component substrings, or tokens.

split

The split method can achieve this by specifying the character(s) that are to be considered the separators between the tokens.

We will learn how to work with lists next lecture, but for now, we'll just print the results directly (which print ca handle).

Let's do some splits:

Example:
phrase = "time consumes everything but it is also the best"
tokens = phrase.split()
print("the phrase '{:s}' is composed of the tokens: {}".format(phrase, tokens))


Try it here

Activity 5 [2 minutes]:
What happens if you change the input to split to the space: " "?


You can use different tokens (I actually wrote "Tolkiens" there!):

url = "https://frankpablo.github.io/classes/csc111/lectures/lectureNotes10.html"
tolkiens = url.split("/")
print("the url '{:s}' is composed of the tokens: {}".format(url, tolkiens))


Try it here

You can also separate into tokens composed of strings (longer than 1 character).
Example:

quote = "Baseball is ninety percent mental and the other half is physical."
tokens = quote.split("al")
print("the quote '{:s}', separated by 'al' is composed of the tokens: {}".format(quote, tokens))


Try it here

join

Another useful task is to join tokens into a longer string.

The format is:

< separator > . join ( < list of tokens > )

Example:

num_str = "1 2 3 4 5 6 7 8 9"
#making the tokens
tokens = num_str.split(" ")
print("the num_str '{:s}'\nis composed of the tokens:\n{}".format(num_str, tokens))
print()
# joining with '+'
num_expr = "+".join(tokens)
print("expression:",num_expr)
# you can use this for something!
print( eval(num_expr) )


Try it here







Homework

[Due for everyone]
Homework 04 (Replit); due on 02/25 before 5 PM

[Optional] TODO