• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/58

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

58 Cards in this Set

  • Front
  • Back

Strings are made up of smaller strings, each containing one ___

character

types comprised of smaller pieces are called

collection data types

a string that contains no characters is called an

empty string

what's the indexing operator?


What is the index?

[] indexing operator


the index is the number inside the indexing operator ex. [4]

give the index of the characters in the following string from left to right and right to left:


"Python"

left to right: 0, 1, 2, 3, 4, 5


right to left: -1, -2, -3, -4, -5, -6

the index of the first character of a string is which number? what about the last character of a string?

first: 0


last: -1

what's the general format for a statement that performs a method on an object?

object.method(value)

is a string an object? if so is it mutable or immutable? (changable or unchangable?)

A string is an immutable object - it cannot be changed.

Make a statement to print out the string "table" in all caps.

print("table".upper())

Make a statement to print out the string "Blue" in all lowercase

print("Blue".lower())

How print the length of a string?

print(len("string"))

Which error will this return? why?


print("fruit"[len("fruit")])


How can you fix it so that it will print the last character of the string (assuming you don't know the length of the string beforehand?)

"IndexError: string index out of range"


len("fruit") returns the length of the string "fruit" starting at 1, so len("fruit") returns 5. But with indexing, the counting starts at 0, so the index of the characters of "fruit" only go up to 4 (t is index of 4). So you try to print "fruit"[5] which doesn't exist.


fixed version:


print("fruit"[len("fruit") - 1])

What is the slice operator?


What does it do?


Give an example using it.

[:]


the slice operator is the :


It returns a part of a string starting from the index to the left of the colon up to but excluding the index to the right of the colon.


Example: "computer"[4:7] returns ute (the characters at indexes 4, 5, and 6)


for the string "computer" write a statement to return the following, using the slice operator for each:


ute


uter


comp


computer

"computer"[4:7]


"computer"[4:]


"computer"[:4]


"computer"[:]

What is lexicographical order?

like alphabetical order except capital letters come before lowercase ex ABCDEabcde



THE LETTERS THAT COME FIRST HAVE A LOWER VALUE such that A < a and d > c

What do the following return:



"apple" < "banana"


"zebra" < "apple"


"Zebra" < "apple"


"Zebra" > "zebra"


"Zebra" == "zebra"


"Zebrafish" > "z"



How are these strings being compared?

True


False


True


False


False


False



the ordinal value of the FIRST character of the string(???) (which is assigned by lexicographical order) is being compared.

HOw is the ordinal value of a character determined?

by its lexicographical order.

How do you find out the ordinal value of a character? (write a statement)

ord("a")


a is random character

How do you find out which character is assigned to a certain ordinal value?

chr(52)


52 is the ordinal value

Does the space character have an ordinal value?

yes! it is 32

Are strings mutable?

NO! STRINGS ARE IMMUTABLE

HOw use the range function to print 0 through 9? write the statement.

for i in range(10):


print(i)

a string is a sequence of...

characters

Write a statement to:


iterate over the char in "apple" and print each character, using the index of each character. pretend you don't know how many characters are in "apple"

for i in range(len("apple")):


print "apple"[i]

Write a statement to:


iterate over and print the first two characters of "apple", using the index of each character. pretend you don't know how many characters are in "apple"

for i in range(len("apple") - (len("apple") - 2)):
print "apple"[i]

How many times is the letter o printed by this statement?

How many times is the letter o printed by this statement?

2

Write a while loop to interate over the string "apple" and print every character. pretend you don't know how many characters are in "apple"

position = 0


while position < len("apple"):


print "apple"[position]


position += 1


How many times is the letter o printed by this statement?


how many times is p printed?

How many times is the letter o printed by this statement?


how many times is p printed?

0, 0

print('p' in 'apple')



What does this print?

True

print('i' in 'apple')



What does this print?

False

print('' in 'apple')



What does this print?

True (an empty string is a substring of any string)

print('a' not in 'apple')



What does this print?

False

print('i' not in 'apple')



What does this print?

True

What is printed by the following?


s = "ball"


r = ""


for item in s:


r = item.upper() + r


print(r)

LLAB



because of the way it's concatenated with the iterated item first

write a function counting the letters of a string input as a parameter

def count(string):


charcount = 0


for i in string:


charcount += 1


return charcount

In the beginning of the function find3 written below, how can we rewrite it to make the parameter start have a default function of 0 (if the parameter isn't specified when the function is called?)



def find3(astring, achar, start)

def find3(astring, achar, start=0)

In the beginning of the function find3 written below, how can we rewrite it to make the parameter end optional - that is, assign the parameter no value at all if it is not entered when the function is called?



def find3(astring, achar, start, end)

def find3(astring, achar, start, end=None)

What is printed?



import string



print(string.ascii_lowercase)

abcdefghijklmnopqrstuvwxyz

What is printed?



import string



print(string.ascii_uppercase)

ABCDEFGHIJKLMNOPQRSTUVWXYZ

What is printed?



import string



print(string.digits)

0123456789

What is printed?



import string



print(string.punctuation)

!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

You do ____ to access a single character in a string using its position (starting from 0).



Write the operator, and Give an example

indexing[]



Example: 'This'[2] evaluates to'i'.


____ returns the number of characters in a string.



Write the operator, and give an example.

len(string)


Example: len('happy') evaluates to 5.

______ a string (with a ___ _____) means accessing each character in the string, one at a time.



Give an example.

traversing (with a for loop)



For example, the following for loop:



for ix in 'Example':

A ____ is a substring of a string.



Write the operator.



Give an example:

slice (or slicing)


[:]



'bananas and cream'[3:6] evaluates to ana (so does'bananas and cream'[1:4]).

The six common comparision operators work with strings, evaluating according to _____ order.

lexigraphical

The _____ operator tests whether one string is contained inside another string.

in

define: collection data type



A data type in which the values are made up of components, or elements, that are themselves values.

define:


default value


The value given to an optional parameter if no argument for it is provided in the function call.

define:


dot notation


Use of the dot operator, ., to access functions inside a module, or to access methods and attributes of an object.

define:


immutable

A compound data type whose elements can not be assigned new values.



define:


index

A variable or value used to select a member of an ordered collection, such as a character from a string, or an element from a list.

define:


optional parameter


A parameter written in a function header with an assignment to a default value which it will receive if no corresponding argument is given for it in the function call.



define:


slice

A part of a string (substring) specified by a range of indices. More generally, a subsequence of any sequence type in Python can be created using the slice operator (sequence[start:stop]).



define:


traverse

To iterate through the elements of a collection, performing a similar operation on each.



define:


whitespace

Any of the characters that move the cursor without printing visible characters. The constantstring.whitespace contains all the white-space characters.

in lexicographical order, capital letters come __ (before/afte) lowercase letters and so have a ____ (lower, higher) value

BEFORE, LOWER

How do you find the index of a substring within a string? (a very quick method can do this)

string.find(substring)


ex.


"string".find("ng")


evaluates to


4