Changes

Jump to: navigation, search

OPS435 Python3 Lab 4

501 bytes added, 22:45, 26 September 2019
PART 2 - Searching and Validating
==PART 2 - Searching and Validating==
:This second part will study how to look for specific character(s) in a string object and also how to verify that a given string object contains the expected certain characters.
:'''Perform the following steps:'''
:#Create a temporay temporary python script with the following contents:<source lang="python">
text = 'Seneca'
letter = text[1]
print(letter)
</source>'text' is a string object which contains a sequence of characters. You can access any characters from the a string object one at a time with the bracket operator '[1]', the . The number (or expression) in the square brackets is called an index and it must be an integer. Note that the first letter in an string object has an index of 0. In the context The use of computer, the index in Python is usually consider as an offset from the beginning of the string, ; and the offset of the first character is zero.
:#Modify the previous python file to:<source lang='python'>
offset = offset + 1
</source>'''len()''' is a built-in function that which returns the number of characters (i.e. the length) if when a string object is passed in to it as an argument. We normally refer this return number as the length of the string object. The maximum offset (i.e. index) a string object can have is its length minus 1. The while loop traverses the string object and displays each character on a line by itself with the offset (index) value that was used to retreive retrieve the character. The loop condition "offset < length" will be '''False''' when offset is equal to the length of the string, and the loop will stop. The last character printed is the one with the offset equal to '''length - 1''', which is the last character in the string object.:#String object is, like a list object, a an iterable object. Each character in a string object can be traversed by using the '''for .. in ..''' construct as followshown in the Python code below:<source lang='"python'">
text = "Seneca"
print(letter)
</source>You can use the above python code as a template to create a function to search for a practicular specific character in a string object. :#Consider the following python scriptcode:<source lang='python'>
def find(text,char):
print(s1,'contains letter S? ->',find(s1,'S'))
</source> The find() function defined in the python script above , is not needed, as it can be replace replaced by the python keyword '''in''', which can be used as a boolean operator. It This boolean operator takes two strings, and return '''True''' if the first appears as a substring in the second.<br/>
Try the following python script:<source lang = 'python'>
print(s1, 'contains letter S? ->', 'S' in s1))
</source>Did it produce the same result as the find() function in previous python code example?:#Try the following python code to search and count for all vowel characters in a string object:<source lang='"python'">
def is_vowel(char):
if char in 'aeiaeiou': return TureTrue
return False
print('There are',vowel_count,'vowel(s) in',text)
</source>Can you create a similar python function called is_digits() to check whether a string object contains only numeric digits? Do you still remember what does the built-in function input() do? It prompts the user on the keyboard and return whatever the user typed as a string!
'''Create a Python script to validate user input'''
</source></li>
<li style="margin-left:25px;">Before proceeding, make certain that you identify all errors in lab4e.py. When the checking script tells you everything is OK - proceed to the next step.</li></ol>
 
= LAB 4 SIGN-OFF (SHOW INSTRUCTOR) =
[[Image:lab1_signoff.png|thumb|right|450px|Students should be prepared with '''all required commands (system information) displayed in a terminal (or multiple terminals) prior to calling the instructor for signoff'''.]]
1,760
edits

Navigation menu