Download presentation
Presentation is loading. Please wait.
1
String Handling
2
String Handling sentence = “The cat sat on the mat” for letter in sentence: #repeats for all letters in sentence variable print(letter)
3
String Handling text =“Sandwich” print(text[2]) #what do you think would happen? Similar in structure to lists. Selects the third character – don’t forget we start with a zero [0] print(text[0:4]) #This starts at zero and displays up to the fourth character. print(text[1:4]) #This starts at one and displays up to the fourth character. a=len(text) print(a) # outputs how many characters are in the variable text
4
String Handling - Tasks
Create a program that counts the number of occurrences of the lower case letter “a” in the variable sentence. Sentence = “The cat sat on the mat” Hint: for letter in sentence is required and then a comparison (IF) to see if that letter ==“a”
5
String Handling - Tasks
Create a program that takes in a password from the user. The length of the password must be more that 6 characters, and the program must output a message to state whether the password is valid or too short. Hint: you shouldn’t need one Development: Loop the program until an acceptable password length is entered.
6
String Handling - Tasks
Task 3 (Tricky!!) Create a program that takes in a five character word from the user and tells them if the word is a palindrome (it is the same backwards and forwards, e.g. “radar”) Hint: you will need to find the length of the word and then compare the first letter with the last letter, the second with the last but one letter [-2], etc. Development (Very tricky): Modify your code so that it does not matter how long the word is. A for loop could be used
7
String Handling - Tasks
Task 4 – A453 Example Extend your Password program (task 2) to include the following rules: At least 6 but no more than 12 characters Error message to explain why failed #not enough characters Strength of password assessed, depending on use of upper, lower case and numeric content. STRONG if all three are used (e.g. RTH34gd) MEDIUM if two types are used (e.g. catman3, RTH42) WEAK if only one type is used (e.g , compmaster, ICT) View next slide for hints.
8
String Handling - Tasks
Task 4 – A453 Example Hints: If letter >=‘a’ and letter <=‘z’ : #this states lower case If letter >=‘A’ and letter <=‘Z’ : #this states upper case If letter >=‘0’ and letter <=‘9’ : #this states number These would need to be placed in a for loop to look through all letters of password. A variable to count how many of each type would also be required before a final comparison to work out the strength of the password.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.