PYTHON CHAPTER 8 POWERPOINT

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What will the following code display? mystring = 'abcdefg' print(mystring[:3]

'abc' because it starts at a which is index 0 and ends at c which is index 2.

What will the following code display? mystring = 'abcdefg' print(mystring[:]

'abcdefg' because everything was displayed.

What will the following code display? mystring = 'abcdefg' print(mystring[2:5])

'cde' because it starts from the 2nd character in the index and goes to the 4th character before stopping.

What will the following code display? mystring = 'abcdefg' print(mystring[3:]

'defg' because it starts at the index of 3 and goes through all of the characters after index 3.

How do you access an individual character in a string? ( Two ways)

* Use a for loop ex: for character in string -Useful when need to iterate over the whole string, such as to count the occurrences of a specific character. * Use indexing -Each character has an index specifying its position in the string, starting at 0. ex: character = my_string[i]

If a string has 10 characters, what is the index of the last character?

9 because the index is counted like: 0,1,2,3,4,5,6,7,8,9 (This totals out to be 10 characters)

Define concatenation

Appending one string to the end of another string -Use the + operator to produce a string that is a combination of its operands.

Define repetition operator

Makes multiple copies of a string and joins them together - The * symbol is a repetition operator when applied to a string and an integer. (string is left operand and the integer/num is the right operand)

STRING METHODS: Define islower()

Returns TRUE if ALL of the alphabetic letters in the string are lowercase, and the string contains at least one alphabetic letter. Otherwise it returns FALSE

STRING METHODS: Define isupper()

Returns TRUE if ALL of the alphabetic letters in the string are uppercase,and the string contains at least one alphabetic letter. Otherwise it returns FALSE

STRING METHODS: Define isspace()

Returns TRUE if the string contains ONLY whitespace characters and is at least one character in length. Otherwise it returns FALSE (Whitespace characters are: spaces, new lines (\n) and tabs (\t). )

STRING METHODS: Define is alnum()

Returns TRUE if the string contains only alphabetic letters OR digits and is at least one character in length. Otherwise it returns FALSE

STRING METHODS: Define isalpha()

Returns TRUE if the string contains only alphabetic letters and is at least one character in length. Otherwise it returns FALSE

STRING METHODS: Define isdigit()

Returns TRUE if the string contains only numeric digits and is at least one character in length. Otherwise it returns FALSE

STRING METHODS: Define lower()

Returns a copy of the string with ALL alphabetic letters converted to lowercase. Any character that is already lowercase,or is not an alphabetic letter is unchanged.

STRING METHODS: Define upper()

Returns a copy of the string with ALL alphabetic letters converted to uppercase. Any character that is already uppercase or is not an alphabetic letter is unchanged.

STRING METHODS: Define strip(char)

Returns a copy of the string with ALL instances of (char) that appear at the BEGINNING and the END of the string removed.

STRING METHODS: Define strip()

Returns a copy of the string with ALL leading and trailing whitespace characters removed

STRING METHODS: Define lstrip()

Returns a copy of the string with ALL leading whitespace characters removed. Leading whitespace characters are: spaces, newlines (\n), and tabs (\t) that appear at the BEGINNING of the string.

STRING METHODS: Define rstrip()

Returns a copy of the string with ALL trailing whitespace characters removed. Trailing whitespace characters are: spaces, newlines (\n), and tabs (\t) that appear at the END of the string.

Define split method

Returns a list containing the words in the string -by default, uses space as separator -can specify a different separator by passing it as an argument to the split method

Define slice

Span of items taken from a sequence, known as a substring

TRUE OR FALSE: Some methods return a copy of the string, to which modifications have been made.

TRUE

TRUE OR FALSE: The len(string) function can be used to obtain the length of a string

TRUE

TRUE OR FALSE: String comparisons are case-sensitive

TRUE -uppercase characters are distinguished from lowercase characters - (lower) and (upper) methods can be used for making case-insensitive string comparisons

TRUE OR FALSE: Strings in python have many types of methods, divided into different types of operations

TRUE ex: mystring.method(arguements) -Some methods test a string for specific characteristics

TRUE OR FALSE: Strings are immutable

TRUE -once they are created they cannot be changed

STRING METHODS: Define lstrip(char)

The (char) arguement is a string containing a character. Returns a copy of the string with ALL instances of (char) that appear at the beginning of the string removed.

STRING METHODS: Define rstrip(char)

The (char) argument is a string containing a character. The method returns a copy of the string with ALL instances of (char) that appear at the END of the string removed.

STRING METHODS: Define replace(old, new)

The (old) and (new) arguments are both strings. The method returns a copy of the string with ALL instances of old replaced by new.

STRING METHODS: Define startswith(substring)

The (substring) argument is a string. Returns TRUE if the string starts with substring

STRING METHODS: Define endswith(substring)

The (substring) argument is a string. The method returns TRUE if the string ends with substring.

STRING METHODS: Define find(substring)

The (substring) argument is a string. The method returns the lowest index in the string where substring is found. If substring is NOT found the method returns -1.

An indexError exception will occur if you try to do what?

Use an index that is out of range for the string

Write a loop that asks the user "Do you want to repeat the program or quit? (R/Q)". The loop should repeat until the user has entered an R or Q (either uppercase or lowercase).

again = input("Do you want to repeat the program or quit? (R/Q)").lower() while again != 'r' and again != 'q': again = input("Do you want to repeat the program or quit? (R/Q)")

Strings are ____, so many of the tools that work with sequences work with strings

sequences

Assume the variable 'name' references a string. Write a for loop that prints each character in the string.

for ch in name: print(ch)

Write code using the 'in' operator that determines whether 'd' is in mystring.

for d in mystring:

Write a loop that counts the number of uppercase characters that appear in the string referenced by the variable 'mystring'.

for letter in mystring: if letter .issuper() count += 1

Write an if statement that displays "Digit" if the string referenced by the variable 'ch' contains a numeric digit. Otherwise, it should display "No digit"

if ch.isdigit(): print("Digit") else: print("No digit")

You can use the __ operator to determine whether one string is contained in another string

in

Assume the variable 'big' references a string. Write a statement that converts the string it references to lowercase and assigns the converted string to the variable 'little'

little = big.lower()

Assume the following statement appears in a program: days = 'Monday Tuesday Wednesday' Write a statement that splits the string, creating the following list: ['Monday', 'Tuesday', 'Wednesday']

my_list = days.split()

Assume the following statement appears in a program: values = 'one$two$three$four' Write a statement that splits the string, creating the following list: ['one', 'two', 'three', 'four']

my_list = values.split($)

You can use the __ operator to determine whether one string is NOT contained in another string

not in

What is the output of the following code? ch = 'a' ch2 = ch.upper() print(ch, ch2)

prints: a, A


संबंधित स्टडी सेट्स

Chapter 38: Caring for Clients With Cerebrovascular Disorders

View Set

Intro to Applied Ecol unit 1 (lectures 1-6)

View Set

Health Assessment Exam 3 (Respiratory)

View Set

Chapter 5: Unit 7 - Marketing Property (Notes)

View Set

Microeconomics Test #2, HPU ECO2030 Principals of Microeconomics - Homework 6, HPU ECO2030 Principals of Microeconomics - Homework 7

View Set

2.10- Hydrogen bonds make liquid water cohesive

View Set

Composite Risk Management Army/Civilian

View Set

plsc1 final quiz Ch 14: New Media

View Set

Descriptive Statistics (Continued)

View Set