Module 8 - Strings Flashcards
How are strings different from integers, floats, and booleans?
A string is a sequence - an ordered collection of other values, where each value is identified by an integer index
How do you access each character in a string?
The bracket operator
How would you access the first letter in banana?
fruit = ‘banana’
letter = fruit[0]
^ selects first character from fruit and assigns it to letter
What is an index?
The expression in brackets
An integer value used to select an item in a sequence, such as a character in a string. In Python indices start from 0
What types of values can you put into a string index?
Value MUST evaluate to an integer, but you can use numbers, operators, and expressions in the brackets: 0 if i=1, can do i+1 2*3 etc..
What is len? What is the syntax for it?
Function that returns the number of characters in a string
size=len(‘fruit’)
print(size)
5
How would you get the last character of a string?
fruit = ‘banana’ #assign value ‘banana’ to var fruit
length = len(fruit) #assign length to var length
last = fruit[length-1] #assign last letter to var last
print(last) #displays ‘a’
remember, b is the zero’th letter!!
you can also use negative indices - fruit[-1] gives you last letter
What does it mean to traverse?
To iterate through the items in a sequence, performing a similar operation on each.
For example, processing a string one character at a time
What is concatenation?
String addition
What is a string slice? What is the syntax?
AKA substring - a segment of a string is called a slice
string [start:end] returns the string from the start (default is 0) up to, but EXCLUDING, the end value
How do you slice from the beginning or to the end?
If you want to slice from the beginning, leave first one blank, [ :3] – it defaults to 0 if blank
If you want to slice to the end, leave last one blank, [3;] – defaults to len(string) if blank
What is the result if the first index is greater than or equal to the second index in a slice? i.e., [3:2] or [3:3]
An empty string
no characters and length 0 but still a string
What happens if you leave both indices empty? i.e., [:]
The entire string is return - not sliced
T or F: Strings are sequences (lists), so many of
the tools that work with sequences work
with strings
True!
What happens if you try to use an index that is out of range for the string?
IndexError exception will occur
sentence=input(“enter your essay”)
for letter in sentence:
Describe what “sentence”, “letter” and “in” are in the code above.
sentence is a SEQUENCE that is a string
letter is a variable that’s introduced in the for-loop
the “in” operator tells us to iterate thru each item in the sequence
Which loop would you use when you need to iterate over the entire string (e.g., count the occurrences of a specific character)?
for loop
Which loop would you use if you want to traverse a part of a string? How would you do it?
while loop
use len to stop loop if we reach the end of the string
use [ ] to refer to particular letters
need a counter to advance index
What happens when you try to change part of a string?
For example,
greeting = “hello world”
greeting[0] = “j”
Error message - TypeError: ‘str’ object does not support item assignment
T or F: Strings are immutable.
True - once they are created, they cannot be changed
The best you can do is create a new string that uses the original
How would you change the first letter of greeting = “hello world” from h to j?
create a new variable
new_greeting = “j” + greeting[1:]
When you put a loop into a function, what happens when you put a return statement INSIDE the loop?
When we get to the return statement, the function breaks out of the loop and returns immediately
What’s the pattern of computation called when we traverse a sequence and return when we find what we are looking for?
Search
If strings are immutable, how come you can concatenate them?
Concatenation doesn’t actually change the existing string, but rather creates a new string and assigns the new string to the previously used variable
Slicing expressions involves a start and end value:
string [start : end]
Can it include a step value?
What are the rules for the values?
Yes, step values can be included
Values can be positive or negative integers (negative indexes used for end of string)
How do you determine whether one string is contained in another string? What about not contained?
Use the ‘in’ operator
General format: string1 in string2
The strings can be string literals or variables referencing strings
You can use ‘not in’ operator for not contained
What is the general format of a string method and operation?
string.method(arguments)
string is either the string (in quotes) or the variable that the string was assigned to
T or F: string comparisons are case-sensitive
True!
lower and upper methods can be used for making case-insensitive string comparisons
What is a method (i.e., string method)?
A method is similar to a function - it takes arguments and returns a value - but the syntax in different
How many arguments does the find method take and what is the syntax?
string.find(“string or slice we’re searching for”, start, end)
not inclusive of end - stops right before it reaches ‘end’ value
start and end are optional arguments