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.

Slides:



Advertisements
Similar presentations
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Advertisements

Java Programming Strings Chapter 7.
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.
Logical Operators and Conditional statements
1.
An Introduction to Textual Programming
Noadswood Science,  To understand what strings are and how decisions are made Thursday, September 17, 2015.
Lists and More About Strings CS303E: Elements of Computers and Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
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.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
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.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Decision Structures, String Comparison, Nested Structures
Validation final steps Stopping gaps being entered in an input.
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
Python Let’s get started!.
1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1.
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.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
More String Manipulation. Programming Challenge Define a function that accepts two arguments: a string literal and a single character. Have the function.
LECTURE 5 Strings. STRINGS We’ve already introduced the string data type a few lectures ago. Strings are subtypes of the sequence data type. Strings are.
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.
Lecture 19 Strings and Regular Expressions
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Containers and Lists CIS 40 – Introduction to Programming in Python
String Processing Upsorn Praphamontripong CS 1110
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
String Manipulation Part 2
Decision Structures, String Comparison, Nested Structures
String Manipulation Chapter 7 Attaway MATLAB 4E.
Chapter 8 More on Strings and Special Methods
CSC 221: Introduction to Programming Fall 2018
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Coding Concepts (Data- Types)
Basic String Operations
15-110: Principles of Computing
Topics Basic String Operations String Slicing
String Processing 1 MIS 3406 Department of MIS Fox School of Business
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Validation using Regular Expressions
Python Strings.
Topics Basic String Operations String Slicing
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
STRING MANUPILATION.
Presentation transcript:

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

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

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

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: 7 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

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

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 bother upper and lowercase letters x = count_letters(“Python is fun”) print(x) >> 11