Download presentation
Presentation is loading. Please wait.
1
String Manipulation Part 2
2
Programming Challenge: Mirror
Write a function entitled “mirror” that reverses a word and returns it back to the user
3
Programming Challenge: RACECAR
A palindrome is a word that when spelled backwards is still the same word Write a program that asks the user for a word Determine whether the word is a palindrome
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 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”)
8
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
9
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
10
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: 4 Digits: 4 Vowels: 5 Consonants: 14
11
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
12
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 Note: These methods must be returned to a location.
13
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
14
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
15
Find and Replace Python has a function called find()
This function returns the index of 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)
16
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!”
17
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
18
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.”
19
Programming Challenge
Write a program that counts the number of letters in a string. You should count both upper and lowercase letters x = count_letters(“Python is fun”) print(x) >> 11
20
ASCII Values Recall that to your computer, all characters are a combination of one’s and zero’s which can represent a numerical decimal point value and in return represent a letter Ex: “A” = 65 Python has a function to retrieve that numerical value for any characters
21
ASCII Values We can use the ord() function to do this
The ord() function takes one argument, a single character as a string (in delimiters) Then, it returns an integer value from the ASCII table which represents that specific character
22
ASCII Values Just remember that because the function returns a value, it must have a place to return it to Example: value = ord(“A”) print(value) >> 65
23
Practice Write a program that asks the user for a word, or any string
Then print out the string, one character at a time with it’s associated ASCII value Give me a name: Donald >> D = 68 o = 111 n = 110 a = 97 l = 108 d = 100
24
ASCII Values We can reverse this process and turn integer values into string characters by using the chr() function The chr() function takes one integer argument and returns it’s character equivalent from the ASCII table as a string Example: x = chr(65) print(x) >> A
25
Programming Challenge
Write a program that generates a random password for the user Passwords must include: At least 10 characters Uppercase AND lowercase letters At least one number
26
Programming Challenge
Write a program that asks the user for a string and encodes it by the following guidelines: If the letter is a vowel, leave it For any other letter, add one to it’s value (i.e. “c” is “d”)
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.