In-Class Coding Lab: Strings

The goals of this lab are to help you to understand:

  • String slicing for substrings
  • How to use Python's built-in String functions in the standard library.
  • Tokenizing and Parsing Data
  • How to create user-defined functions to parse and tokenize strings

Strings

Strings are immutable sequences

Python strings are immutable sequences.This means we cannot change them "in part" and there is impicit ordering.

The characters in a string are zero-based. Meaning the index of the first character is 0.

We can leverage this in a variety of ways.

For example:


In [ ]:
x = input("Enter something: ")
print ("You typed:", x)
print ("number of characters:", len(x) )
print ("First character is:", x[0])
print ("Last character is:", x[-1])

## They're sequences, so you can loop definately:
print("Printing one character at a time: ")
for ch in x:
    print(ch) # print a character at a time!

Slices as substrings

Python lists and sequences use slice notation which is a clever way to get a substring from a given string.

Slice notation requires two values: A start index and the end index. The substring returned starts at the start index, and ends at the position before the end index. It ends at the position before so that when you slice a string into parts you know where you've "left off".

For example:


In [ ]:
state = "Mississippi"
print (state[0:4])          # Miss
print (state[4:len(state)]) # issippi

In this next example, play around with the variable split adjusting it to how you want the string to be split up. Re run the cell several times with different values to get a feel for what happens.


In [ ]:
state = "Mississippi"
split = 4   # TODO: play around with this number
left = state[0:split]
right = state[split:len(state)]
print(left, right)

Slicing from the beginning or to the end

If you omit the begin or end slice, Python will slice from the beginnning of the string or all the way to the end. So if you say x[:5] its the same as x[0:5]

For example:


In [ ]:
state = "Ohio"
print(state[0:2], state[:2]) # same!
print(state[2:len(state)], state[2:]) # same

Now Try It!

Split the string "New Hampshire" into two sub-strings one containing "New" the other containing "Hampshire" (without the space).


In [ ]:
## TODO: Write code here

Python's built in String Functions

Python includes several handy built-in string functions (also known as methods in object-oriented parlance). To get a list of available functions, use the dir() function on any string variable, or on the type str itself.


In [ ]:
print ( dir(str))

Let's suppose you want to learn how to use the count function. There are 2 ways you can do this.

  1. search the web for python 3 str count or
  2. bring up internal help help(str.count)

Both have their advantages and disadvanges. I would start with the second one, and only fall back to a web search when you can't figure it out from the Python documenation.

Here's the documentation for count


In [ ]:
help(str.count)

You'll notice in the help output it says S.count() this indicates this function is a method function. this means you invoke it like this variable.count().

Now Try It

Try to use the count() function method to count the number of 'i''s in the string 'Mississippi:


In [ ]:
state = 'Mississippi'
#TODO: use state.count

TANGENT: The Subtle difference between function and method.

You'll notice sometimes we call our function alone, other times it's attached to a variable, as was the case in previous example. When we say state.count('i') the period (.) between the variable and function indicates this function is a method function. The key difference between a the two is a method is attached to a variable. To call a method function you must say variable.function() whereas when you call a function its just function(). The variable associated with the method call is usually part of the function's context.

Here's an example:


In [ ]:
name = "Larry"
print( len(name) )      # a function call len(name) stands on its own. Gets length of 'Larry'
print( name.__len__() ) # a method call name.__len__()  does the name thing for its variable 'Larry'

Now Try It

Try to figure out which built in string function to use to accomplish this task.

Write some code to find the text 'is' in some text. The program shoud output the first position of 'is' in the text.

Examples:

When: text = 'Mississippi' then position = 1
When: text = "This is great" then position = 2
When: text = "Burger" then position = -1

In [ ]:
# TODO: Write your code here
text = input("Enter some text: ")

Now Try It

Is that a URL?

Try to write a rudimentary URL checker. The program should input a text string and then use the startswith function to check if the string begins with "http://" or "https://" If it does we can assume it is a URL. Your program should output whether or not the input text string was a URL.

Example:

Enter some text: http://www.google.com
http://www.google.com is a URL.

In [ ]:
## TODO: write code here:

Metacognition

Please answer the following questions. This should be a personal narrative, in your own voice. Answer the questions by double clicking on the question and placing your answer next to the Answer: prompt.

Questions

  1. Record any questions you have about this lab that you would like to ask in recitation. It is expected you will have questions if you did not complete the code sections correctly. Learning how to articulate what you do not understand is an important skill of critical thinking.

Answer:

  1. What was the most difficult aspect of completing this lab? Least difficult?

Answer:

  1. What aspects of this lab do you find most valuable? Least valuable?

Answer:

  1. Rate your comfort level with this week's material so far.

1 ==> I can do this on my own and explain how to do it.
2 ==> I can do this on my own without any help.
3 ==> I can do this with help or guidance from others. If you choose this level please list those who helped you.
4 ==> I don't understand this at all yet and need extra help. If you choose this please try to articulate that which you do not understand.

Answer:


In [ ]:
# SAVE YOUR WORK FIRST! CTRL+S
# RUN THIS CODE CELL TO TURN IN YOUR WORK!
from ist256.submission import Submission
Submission().submit()