Module 8 - Strings Flashcards

1
Q

How are strings different from integers, floats, and booleans?

A

A string is a sequence - an ordered collection of other values, where each value is identified by an integer index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you access each character in a string?

A

The bracket operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you access the first letter in banana?

A

fruit = ‘banana’
letter = fruit[0]
^ selects first character from fruit and assigns it to letter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an index?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What types of values can you put into a string index?

A
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..
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is len? What is the syntax for it?

A

Function that returns the number of characters in a string
size=len(‘fruit’)
print(size)
5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you get the last character of a string?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does it mean to traverse?

A

To iterate through the items in a sequence, performing a similar operation on each.
For example, processing a string one character at a time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is concatenation?

A

String addition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a string slice? What is the syntax?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you slice from the beginning or to the end?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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]

A

An empty string

no characters and length 0 but still a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens if you leave both indices empty? i.e., [:]

A

The entire string is return - not sliced

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

T or F: Strings are sequences (lists), so many of
the tools that work with sequences work
with strings

A

True!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happens if you try to use an index that is out of range for the string?

A

IndexError exception will occur

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

sentence=input(“enter your essay”)
for letter in sentence:

Describe what “sentence”, “letter” and “in” are in the code above.

A

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

17
Q

Which loop would you use when you need to iterate over the entire string (e.g., count the occurrences of a specific character)?

A

for loop

18
Q

Which loop would you use if you want to traverse a part of a string? How would you do it?

A

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

19
Q

What happens when you try to change part of a string?
For example,
greeting = “hello world”
greeting[0] = “j”

A

Error message - TypeError: ‘str’ object does not support item assignment

20
Q

T or F: Strings are immutable.

A

True - once they are created, they cannot be changed

The best you can do is create a new string that uses the original

21
Q

How would you change the first letter of greeting = “hello world” from h to j?

A

create a new variable

new_greeting = “j” + greeting[1:]

22
Q

When you put a loop into a function, what happens when you put a return statement INSIDE the loop?

A

When we get to the return statement, the function breaks out of the loop and returns immediately

23
Q

What’s the pattern of computation called when we traverse a sequence and return when we find what we are looking for?

A

Search

24
Q

If strings are immutable, how come you can concatenate them?

A

Concatenation doesn’t actually change the existing string, but rather creates a new string and assigns the new string to the previously used variable

25
Q

Slicing expressions involves a start and end value:
string [start : end]
Can it include a step value?
What are the rules for the values?

A

Yes, step values can be included

Values can be positive or negative integers (negative indexes used for end of string)

26
Q

How do you determine whether one string is contained in another string? What about not contained?

A

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

27
Q

What is the general format of a string method and operation?

A

string.method(arguments)

string is either the string (in quotes) or the variable that the string was assigned to

28
Q

T or F: string comparisons are case-sensitive

A

True!

lower and upper methods can be used for making case-insensitive string comparisons

29
Q

What is a method (i.e., string method)?

A

A method is similar to a function - it takes arguments and returns a value - but the syntax in different

30
Q

How many arguments does the find method take and what is the syntax?

A

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