Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
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.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Python for Informatics: Exploring Information
Python Programming Chapter 7: Strings Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
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.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
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.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
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.
String and Lists Dr. José M. Reyes Álamo.
List Algorithms Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lecture 19 Strings and Regular Expressions
String Processing Upsorn Praphamontripong CS 1110
CS 115 Lecture 8 Structured Programming; for loops
Variables, Expressions, and IO
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Strings Part 1 Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lists Part 1 Taken from notes by Dr. Neil Moore
CS 106A, Lecture 9 Problem-Solving with Strings
Introduction to Strings
Introduction to Strings
String Manipulation Part 2
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
List Algorithms Taken from notes by Dr. Neil Moore
Lists in Python.
CHAPTER THREE Sequences.
Using files Taken from notes by Dr. Neil Moore
Chapter 8 More on Strings and Special Methods
Python - Strings.
Data types Numeric types Sequence types float int bool list str
Chapter 8 More on Strings and Special Methods
CSC 221: Introduction to Programming Fall 2018
CEV208 Computer Programming
Module 4 Loops.
String and Lists Dr. José M. Reyes Álamo.
Coding Concepts (Data- Types)
Lists Part 1 Taken from notes by Dr. Neil Moore
Basic String Operations
Introduction to Strings
CS 1111 Introduction to Programming Spring 2019
For loops Taken from notes by Dr. Neil Moore
Topics Basic String Operations String Slicing
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Introduction to Strings
Python Review
Python Strings.
Topics Basic String Operations String Slicing
Class code for pythonroom.com cchsp2cs
Introduction to Strings
Strings Part 2 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
Presentation transcript:

Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen CS 115 Lecture 11 Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen

Strings We’ve been using strings for a while. What can we do with them? Read them from the user: mystr = input(“Name? “) Print them to the screen: print(mystr) Convert (type-cast) them into ints or floats: num = int(userin) Concatenate them with +: name = first + “ “ + last Compare with other strings: if “A” <= name <= “K”: Check whether they are all digits: if mystr.isdigit():

Strings in detail Let’s see how to do more things with strings: Find the length of a string Get individual characters that are in a string Extract ranges of characters (“slicing”) Convert a string to upper/lower case Search for characters or substrings in strings Search and replace substrings Remove whitespace from strings

String length The length of a string is the number of characters in it. Spaces count! So do newlines and other escaped characters To get the length of a string, use the len function: name = “HAL 9000” numchars = len(name) # that’s 8 characters Argument type: string Return type: integer What’s len(“”)? zero We’ll see later that len works with lists too.

Extracting individual characters from a string The characters in a string are numbered from 0 to length -1 HAL 9000 (length = 8) 01234567 Each number is called a position or index or subscript of the character You can use square brackets to get the character at a given position first = name[0] # this is “H” This is called subscripting or indexing The position must be smaller than the length print(name[8]) # ERROR: string index out of range

Extracting characters with negative subscripts You can subscript with negative numbers, counting from the right end name[-1] is the last, rightmost character name[-2] is the next to last character … name[-len(name)] is the first, left most character name[-i] is the same character as name[len(name) –i] name[-9] is still out of range!

Extracting substrings: slicing The square-bracket notation also lets us extract multiple characters. HAL 9000 (length = 8) 01234567 For example, “the first 3 characters” or “characters 2 through 4” or “the fifth character” (a substring can be only one character long, or can be empty too!) Subscript using a slice (“slicing”) Syntax: start position, a colon “:”, and stop position (one-past-the-end) Similar semantics to range (start, stop) The first three characters: name[0:3] # is “HAL” “Start at character 0 and stop before character 3”

Extracting substrings: slicing Characters two through four: name[2:5] # is “L 9” You can leave out either the start or the stop position (or both!) Leaving out the start position means “start at the 0th character” first = name[:3] # “HAL” Leaving out the stopping position means “go all the way to the end of the string” last = name[4:] # “9000” Leaving out both means “the whole string” (seems silly here) copy = name[:] # “HAL 9000” Slicing does NOT change the original string, it makes (returns) a new one!

Converting case Python strings have several methods to change their capitalization (case) These methods don’t change the original string! They return a NEW string, so use them with assignment statements Example: name=“albert Einstein” All lowercase: lazy = name.lower() # lazy is “albert einstein” All uppercase: telegraph = name.upper() # telegraph is “ALBERT EINSTEIN” First letter uppercase: almost = name.capitalize() # almost is “Albert einstein” First letter of each word uppercase: nice = name.title() # nice is “Albert Einstein”

Converting case One use for converting case methods case-insensitive comparision Asking for yes/no The user might type in “Y” or “y” or “N” or “n” Convert the input to all uppercase and compare that if userin.upper() == “Y” # handles “y” too You can use a subscript to handle multi-character inputs if userin[0].upper() == “Y” # handles “YES” or “Yes” or “Yep” or …

Searching inside a string Python has two ways for searching inside a string, looking for a substring The in operator: needle in haystack needle and haystack are both string variables (can also be lists) Returns a boolean if “ “ in name: # True if name contains a space The substring can appear anywhere in the string if “CS” in class: # True for CS115, SCSI, 1CS Case-sensitive! if “cs” in “CS115”: # False! It must be contiguous: if “C1” in “CS115”: # False!

Searching inside a string Sometimes you need to know not just whether the substring is there, but also where it is. The find method returns the location of a substring pos = haystack.find(needle) Find the first occurrence of the needle in the haystack Returns the position where it was found (0 = first position, etc) Returns -1 if the search string is not found You can use another argument to start searching in the middle: pos = haystack.find(needle, 4) # start looking at position 4 In a loop you can use the last match + 1 sp1 = haystack.find(“ “) # first space in haystack sp2 = haystack.find(“ “, sp1 + 1) # second space in haystack Watch out – if first search fails, sp1 = -1! sp2 would be searching from same location as sp1

Searching inside a string rfind is similar, but searches backwards, from the right end to the left So rfind finds the last occurrence in a string text = “the last space here” lastsp = text.rfind(“ “) # 14

Combining find and slicing You can use find and slicing to extract part of a string: space = name.find(“ “) if space != -1: first = name[:space] # string before the space last = name[space+1:] # string after the space Exercise : find all the words in a string (line of words sentence)

Search and replace Often you don’t really care where the substrings are, but just want to replace them with something else You can use the replace method newstr = str.replace(“from”,”to”) Finds all the occurrences of “from” and replaces them with “to”. Does not modify the original string, it returns a new string You can tell replace to only replace a certain number of occurrences course = “CS 115 Introduction to Programming” print(course.replace(“ “, “-”, 1)) # just the first occurrence would print “CS-115 Introduction to Programming”

Strip When getting input from a user or a file, sometimes there is extra whitespace The strip method removes whitespace from the beginning and the end of the string Whitespace: space, tab, newline (and some other exotic characters) Does not affect whitespace in the middle of the string! Does not change the original string, it returns a new one userin = “˽˽\tCS˽˽115˽\n” # ˽ means space clean = userin.strip() # gives “CS˽˽115”

Strip Can strip from only the left end or right end with lstrip and rstrip lclean = userin.lstrip() # “CS˽˽115˽\n” rclean = userin.rstrip() # “˽˽\tCS˽˽115” print(userin) # what does this print? ……..

Traversing strings The for loop in Python can iterate not only over integers but also over the characters in a string: for char in name: Called “iterating over” or traversing (“walking across”) the string As usual char is the name of a new variable (in line above) In each iteration of the loop, char will be one character In order char is NOT a number! So if name = “Hal” The first time, char = “H” Second time, char = “a” Last time, char = “l”

String traversal examples Let’s write a couple programs using strings and for loops to: Check to see if a string contains a digit. How is this different from string.isdigit()? isdigit checks to see if all the characters are digits hasdigit.py Remove vowels from a string Remember, we cannot modify the original string So we’ll need to build a new string for the result We’ll concatenate to this new string to add on the letters we want The string will be a kind of accumulator

Iterating with an index Traversing a string gives you the characters but not their positions! That’s fine for many uses, but sometimes you do care about the position There are three ways to do this: Loop over the string and keep a counter going Initialize the counter to zero (start at left end of string) Use the same loop as before, for char in name: Increment the counter at the end of each iteration

Iterating with an index (cont’d) Loop over the range of indices for i in range(len(name)): Inside the loop, name[i] gives the character at that index Use enumerate to get both character and index at the same time for i, char in enumerate(name): Each iteration, i will be the index … and char will be the character at that position

Iterating with an index Let’s change our “hasdigit” function to “finddigit” in three ways. finddigit-counter.py finddigit-range.py finddigit-enumerate.py