String Manipulation Part 2

Slides:



Advertisements
Similar presentations
Java Programming Strings Chapter 7.
Advertisements

String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
1.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Noadswood Science,  To understand what strings are and how decisions are made Thursday, September 17, 2015.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
Python for Informatics: Exploring Information
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
More Strings CS303E: Elements of Computers and Programming.
Decision Structures, String Comparison, Nested Structures
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
Python Let’s get started!.
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
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.
Strings Characters and Sentences 1.Overview 2.Creating Strings 3.Slicing Strings 4.Searching for substrings 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
More String Manipulation. Programming Challenge Define a function that accepts two arguments: a string literal and a single character. Have the function.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Last of String Manipulation. Programming Challenge Write a program that asks the user for a string and encodes it by the following guidelines: –If the.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Strings in Python String Methods. String methods You do not have to include the string library to use these! Since strings are objects, you use the dot.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
The Methods and What You Need to Know for the AP Exam
String and Lists Dr. José M. Reyes Álamo.
String Manipulation Part 1
String Methods Programming Guides.
Strings CSCI 112: Programming in C.
Introduction to Python
Repetition Structures
Lecture 19 Strings and Regular Expressions
CSC 131: Introduction to Computer Science
Python Let’s get started!.
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Formatting Output.
Containers and Lists CIS 40 – Introduction to Programming in Python
More on Lists.
Strings Part 1 Taken from notes by Dr. Neil Moore
Representing Characters
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
Decision Structures, String Comparison, Nested Structures
Decision Structures, String Comparison, Nested Structures
Chapter 8 More on Strings and Special Methods
Python - Strings.
Chapter 8 More on Strings and Special Methods
CSC 221: Introduction to Programming Fall 2018
Repetition Structures
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Coding Concepts (Data- Types)
CHAPTER 3: String And Numeric Data In Python
15-110: Principles of Computing
Topics Basic String Operations String Slicing
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Introduction to Computer Science
Python Strings.
Topics Basic String Operations String Slicing
Variables in C Topics Naming Variables Declaring Variables
Formatting Output.
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
STRING MANUPILATION.
Presentation transcript:

String Manipulation Part 2

Programming Challenge: Mirror Write a function entitled “mirror” that reverses a word and returns it back to the user

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

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”)

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!”)

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!”

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”)

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

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

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

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

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.

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

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

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)

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!”

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

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.”

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

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

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

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

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

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

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

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”)