The goals of this lab are to help you to understand:
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!
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)
In [ ]:
state = "Ohio"
print(state[0:2], state[:2]) # same!
print(state[2:len(state)], state[2:]) # same
In [ ]:
## TODO: Write code here
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.
python 3 str count
orhelp(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)
In [ ]:
state = 'Mississippi'
#TODO: use state.count
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'
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: ")
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:
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.
Answer:
Answer:
Answer:
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()