Presentation is loading. Please wait.

Presentation is loading. Please wait.

Last of String Manipulation. Programming Challenge: Usernames Let’s say you work at the IT department for NYU and you are asked to generate student ID’s.

Similar presentations


Presentation on theme: "Last of String Manipulation. Programming Challenge: Usernames Let’s say you work at the IT department for NYU and you are asked to generate student ID’s."— Presentation transcript:

1 Last of String Manipulation

2 Programming Challenge: Usernames Let’s say you work at the IT department for NYU and you are asked to generate student ID’s for the incoming freshman class ID’s are created as follows: –The first four characters of a student’s last name –The first character of a student’s first name –The last two digits of the year in which they graduated high school

3 Programming Challenge: Usernames Write a program that asks the user for their first name, last name and their year of graduation Then, create a password that follows the guidelines and print it out to the user Note: if the user’s last name is less than 4 characters, then you should use their entire last name

4 Testing Strings with “In” and “Not in” You can test to see if a substring exists inside of another string by using the “in” operator In other words, you can find words within a string Example: Kleinfelder = “Tyler Nicola Sam Kevin” if “Sam” in Kleinfelder: print (“I found him!”) else: print(“he’s not here”)

5 Testing Strings with “In” and “Not in” We can also check to see if a substring is NOT in another string (you may have thought to yourself already, this is silly as we could do this with the IN operator anyway …) Example: Kleinfelder = “Tyler Nicola Sam Kevin” if “Gabe” not in Kleinfelder: print (“he’s not a part of squad!”) else: print(“he somehow joined the crew!”)

6 Programming Challenge: Safe PW’s Now a days when you make a password, the program will not allow you to make a password that has your username in it Write a program that replicates this type of program: Example: username: theDonz password: theDonz0987 “Sorry, that’s not a valid password!”

7 String Methods A “method” is a function that belongs to a particular “object” and performs an operation on only that object The syntax for methods is similar to that of calling functions from a module string_variable_name. method_name(arguments) Note: Arguments are rarely necessary in calling methods

8 String Testing Methods String testing methods allow you to determine if certain patterns exist within a given string variable number = “1234” if number.isdigit() == True: print(“All characters are digits!”) else: print(“Not all characters are digits”)

9 String Testing Methods number = “1234” if number.isdigit() == True: print(“All characters are digits!”) else: print(“Not all characters are digits”) Notice we are checking to see if all characters are digits and if so, the method returns True

10 String Testing Methods variable.isalnum()#checks if characters are alphanumeric variable.isalpha()#checks if characters are all alphabetic variable.isdigit()#checks if characters are all digits variable.isspace()#checks if all characters are “whitespace” variable.isupper()#checks if all characters are upper case

11 Programming Challenge Write a program that counts the # of spaces, digits, vowels, and consonants in a string that user inputs >> Enter a phrase: Sally Sells 1000 sea shells. Spaces: 7 Digits: 4 Vowels: 5 Consonants: 14

12 Modification Methods Strings are immutable but we saw that we can slice portions of strings and output it in a new string We can also modify the characters in a string and create a new one We know a couple of these: string.upper(); string.lower() word = “DoNaLd” new_word = word.lower() print(new_word) >> donald

13 Modification Methods lower() #returns all lowercase version upper()#returns all uppercase version rstrip()#removes all whitespace at end of string lstrip()#removes all leading whitespace characters capitalize()#returns string with first character capitalized title()#returns string with first character of each word capitalized swapcase() #switches all upper to lowercase and vice versa

14 Programming Challenge Write a program that accepts a phrase from the user Strip all leading and trailing “white space” from the string If the string is an even number of characters, make it all lowercase If the string is an odd number of characters, make it all uppercase

15 Find and Replace Microsoft Word has a function called “Find and Replace” in which it searches the documents for a specified string and replaces it with whatever you want it to

16 Find and Replace Python has a function called find() This function returns the index of a the first occurrence of the substring you are looking for If it cannot find the string, it will return -1 word = “Like finding a needle in a haystack!” location = word.find(“needle”) print(location)

17 He Who Shall Not Be Named Write a program that asks the user for a phrase. If they mention, HE WHO SHALL NOT BE NAMED, you should write: “YOU DIE!”

18 He Who Shall Not Be Named You can also replace occurrences of a particular substring, by using the replace() method word = “Voldemort had one goal in life – to kill Harry Potter” new = word.replace(“Voldemort”, “He who shall not be named”) print(new) >> He who shall not be named had one goal in life – to kill Harry Potter

19 Programming Challenge Let’s say I’m writing your college recommendation, and I don’t really care about you … write me a program that replaces all the names with a new name Note: You should also change all of “his” with the pronoun “her” if necessary Recommendation: “Matt is such a great student. From his test scores, to his behavior, Matt was a star pupil.”

20 Programming Challenge Write a program that counts the number of letters in a string. You should count bother upper and lowercase letters x = count_letters(“Python is fun”) print(x) >> 11


Download ppt "Last of String Manipulation. Programming Challenge: Usernames Let’s say you work at the IT department for NYU and you are asked to generate student ID’s."

Similar presentations


Ads by Google