for Loop with Stringsin and not inin Operator on a FileThe final exam will be given on Thursday, May 21st. It will be on Blackboard. I will use the same format as the Midterm. The exam will be a two parts exam (Questions and writing short segments of Python code).
The last day of our class will be on Tuesday, May 12th, We will have an optional Zoom meeting for any question you may have. I will send you the Zoom information.
Class 28, on Thursday, May 7th, will be a review session.
You will only be responsible for the material in Class Notes 28 and the review for the Mid-term, which you will find here.
The final will be an online exam.
>>> team = "Red Sox"
>>> team[0] 'R'
>>> letters = 'abcde'
>>> len(letters)
5
>>> letters[4]
'e'
>>> letters[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> letters[-1] 'e' >>> letters[-2] 'd' >>> letters[-3] 'c' >>> letters[-4] 'b' >>> letters[-5] 'a' >>> letters[-len(letters)] 'a'
>>> numbers = [1, 2, 3, 4, 5] >>> numbers[-1] 5
>>> values = (1, 2, 3, 4, 5) >>> values[-1] 5
for Loop with Stringsfor loop
>>> for ch in team: ... print(ch) ... R e d S o x
>>> def character_count(string, char):
... count = 0
... for ch in string:
... if ch == char:
... count += 1
... return count
...
>>> character_count("Mississippi", "i")
4
>>> character_count("Mississippi", "s")
4
>>> character_count("Mississippi", "p")
2
for loop to reverse a string
>>> def string_reverse(string):
... new_string = ""
... for ch in string:
... new_string = ch + new_string
... return new_string
...
>>> string_reverse('Mississippi')
'ippississiM'
>>> string_reverse('radar')
'radar'
>>> string_1 = "Red" >>> string_1 = string_1 + " Sox" >>> string_1 'Red Sox'
string_1 += " Sox"two things are happening
>>> string_1 'Red Sox' >>> string_1 = "Go " + string_1 >>> string_1 'Go Red Sox'
>>> team = "Boston Red Sox"
>>> team[0:6] 'Boston'
IT 116 Scores Class Exercise 24 ====================================================== __ Elvis Barbosa __ Eddie Beazer __ George Boamah __ Vincent Chen ...
in and not inin operatorin operator when studying listsin operator returns True when a value is an element in a list or tuple
>>> numbers = [1, 2, 3, 4, 5] >>> 2 in numbers True >>> "foo" in numbers False
in operator on a string
>>> team = "Red Sox" >>> "R" in team True >>> "z" in team False
in operator has another trick up its sleeve when used with strings
>>> "Red" in team True >>> "Reds" in team False
not in
>>> "Red" not in team False >>> "Reds" not in team True
not innot is a
logical operator
it is not used that way here
not as a logical operator with the in operator in
>>> not "Red" in team False >>> not "Reds" in team True
not in is a single operator
that returns the opposite of in
in Operator on a Filein operator
for loop
for line in file:
if "__" in line:
add to no script list
elif "EE" in line:
add to not executable list
elif "SS" in line:
add to syntax error list
elif "XX" in line:
add to error list
| Method | Description |
|---|---|
| isalnum() | Returns True if the string contains
only alphabetic letters or digits and has a length of at least 1. Returns False otherwise. |
| isalpha() | Returns True if the string contains
only alphabetic letters and has a length of at least 1. Returns False otherwise. |
| isdigit() | Returns True if the string contains only digits
and has a length of at least 1. Returns False otherwise. |
| islower() | Returns True if all of the alphabetic letters
in the string are lowercase and has a length of at least 1. Returns False otherwise. |
| isspace() | Returns True if the string contains only whitespace
characters and has a length of at least 1. Returns False otherwise. |
| isupper() | Returns True if all of the alphabetic
letters in the string are uppercase and has a length of at least 1. Returns False otherwise. |
True when all
the characters are letters or digits
>>> first_name 'Glenn' >>> first_name.isalnum() True >>> digits '0123456789' >>> digits.isalnum() True >>> user 'alpha26' >>> user.isalnum() True
False if the string contains a space
>>> team = 'Red Sox' >>> team 'Red Sox' >>> team.isalnum() False
>>> jumble 'adbcd1234!@#$%' >>> jumble.isalnum() False
>>> empty '' >>> empty.isalnum() False
True when all the characters
are letters
>>> first_name 'Glenn' >>> first_name.isalpha() True >>> digits '0123456789' >>> digits.isalpha() False
>>> user 'alpha26' >>> user.isalpha() False >>> team 'Red Sox' >>> team.isalpha() False
False when the string is empty
>>> empty '' >>> empty.isalpha() False
True when all the characters
are digits
>>> digits '0123456789' >>> digits.isdigit() True >>> user 'alpha26' >>> user.isdigit() False
False when the string is empty
>>> empty '' >>> empty.isdigit() False
True when all the alphabetic
the characters are lower case
>>> noun 'watch' >>> noun.islower() True >>> first_name 'Glenn' >>> first_name.islower() False
>>> user 'alpha26' >>> user.islower() True >>> jumble 'adbcd1234!@#$%' >>> jumble.islower() True
False
if there are no letters in the string
>>> digits '0123456789' >>> digits.islower() False >>> empty '' >>> empty.islower() False
True when all the
alphabetic the characters are upper case
>>> league 'MLB' >>> league.isupper() True >>> standard 'HTML 5' >>> standard.isupper() True >>> team 'Red Sox' >>> team.isupper() False
False when no letters are present in the string
>>> digits '0123456789' >>> digits.isupper() False >>> empty '' >>> empty.isupper() False
True when all the characters
are whitespace
>>> whitespace ' \t\n' >>> whitespace.isspace() True
>>> team 'Red Sox' >>> team.isspace() False
>>> empty '' >>> empty.isspace() False
| Method | Description |
|---|---|
| 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. |
| 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. |
| lstrip() | Returns a copy of the string with leading characters removed. When run without an argument, it removes leading whitespace characters. When run with an argument, removes those characters. |
| rstrip() | Returns a copy of the string with all trailing characters removed. When run without an argument, it removes tailing whitespace characters. When run with an argument, removes those characters. |
| strip() | Returns a copy of the string with leading and trailing characters removed. When run without an argument, it removes leading and tailing whitespace characters. When run with an argument, removes those characters. |
>>> team 'Red Sox' >>> team.lower() 'red sox' >>> name 'Glenn' >>> name.lower() 'glenn' >>> league 'NFL' >>> league.lower() 'nfl'
>>> jumble 'adbcd1234!@#$%' >>> jumble.lower() 'adbcd1234!@#$%' >>> user 'alpha26 >>> user.lower() 'alpha26'
>>> name 'Glenn' >>> name.upper() 'GLENN' >>> noun 'leaf' >>> noun.upper() 'LEAF'
>>> jumble 'adbcd1234!@#$%' >>> jumble.upper() 'ADBCD1234!@#$%'
>>> str1 ' foo ' >>> str1.lstrip() 'foo ' >>> str2 '\t\tfoo\t\t' >>> str2.lstrip() 'foo\t\t' >>> str3 '\n\nfoo\n\n' >>> str3.lstrip() 'foo\n\n'
>>> str4
'---foo---'
>>> str4.lstrip("-")
'foo---'
>>> str5 = '- - - foo- - - '
>>> str5.lstrip('- ')
'foo- - - '
>>> str1 ' foo ' >>> str1.rstrip() ' foo' >>> str2 '\t\tfoo\t\t' >>> str2.rstrip() '\t\tfoo' >>> str3 '\n\nfoo\n\n' >>> str3.rstrip() '\n\nfoo'
>>> str4
'---foo---'
>>> str4.rstrip("-")
'---foo'
>>> str5
'- - - foo- - - '
>>> str5.rstrip("- ")
'- - - foo'
>>> str1 ' foo ' >>> str1.strip() 'foo' >>> str2 '\t\tfoo\t\t' >>> str2.strip() 'foo' >>> str3 '\n\nfoo\n\n' >>> str3.strip() 'foo'
>>> str4
'---foo---'
>>> str4.strip("-")
'foo'
>>> str5
'- - - foo- - - '
>>> str5.strip("- ")
'foo'
| Method | Description |
|---|---|
| endswith(substring) | The method returns True if the string ends with the substring. |
| startswith(substring) | The method returns True if the string starts with the substring. |
| find(substring) | The method returns the lowest index in the string where substring is found. If string is not found, the method returns −1. |
| replace(old_substring, new_substring) | The method returns a copy of the string with all instances of old_ubstring replaced by new_substring. |
True the substring
is at the end of the string
>>> team = 'Red Sox'
>>> team.endswith('Red')
False
>>> team.endswith('Sox')
True
>>> team.endswith(' Sox')
True
True the substring
is at the start of the string
>>> team
'Red Sox'
>>> team.startswith('Sox')
False
>>> team.startswith('Red')
True
>>> team
'Red Sox'
>>> team.find('Red')
0
>>> team.find(' ')
3
>>> team.find('Sox')
4
>>> nonsense
'foo bar bletch foo'
gt;>> nonsense.find('foo')
0
>>> team.find('Yankees')
-1
>>> nonsense
'foo bar bletch foo'
>>> nonsense.replace('foo', 'bam')
'bam bar bletch bam'
>>> team.replace('Six','Socks')
'Red Sox'
Honor confidentiality