More Strings CS303E: Elements of Computers and Programming.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
1.
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.
Python Programming Chapter 7: Strings Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Strings CS303E: Elements of Computers and Programming.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
12/9/2010 Course A201: Introduction to Programming.
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.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Processing Text Excel can not only be used to process numbers, but also text. This often involves taking apart (parsing) or putting together text values.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
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.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
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.
Nested Loops CS303E: Elements of Computers and Programming.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
Introduction to programming in java
Introduction to Programming
String and Lists Dr. José M. Reyes Álamo.
Input and Output Upsorn Praphamontripong CS 1110
Module 4 String and list – String traversal and comparison with examples, List operation with example, Tuples and Dictionaries-Operations and examples.
String Processing Upsorn Praphamontripong CS 1110
CS 106A, Lecture 9 Problem-Solving with Strings
Introduction to Strings
Introduction to Strings
MSIS 655 Advanced Business Applications Programming
String Manipulation Part 2
Chapter 10 Lists.
Python - Strings.
String Manipulation Chapter 7 Attaway MATLAB 4E.
4. sequence data type Rocky K. C. Chang 16 September 2018
Introduction to Programming
CSE 1020:Software Development
CSC 221: Introduction to Programming Fall 2018
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Basic String Operations
Introduction to Strings
CS 1111 Introduction to Programming Spring 2019
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
String methods 26-Apr-19.
Introduction to Strings
Introduction to Computer Science
Python Strings.
Topics Basic String Operations String Slicing
Exam Prep.
Introduction to Strings
Topics Basic String Operations String Slicing
What We Want To Do User enters: Mary Smith
Presentation transcript:

More Strings CS303E: Elements of Computers and Programming

Exams Exams will be returned today. Note that we will not have discussion section meetings on Friday. Exams will be returned today. Note that we will not have discussion section meetings on Friday. You have until the Monday after spring break to request any regrades. You have until the Monday after spring break to request any regrades.

Iterating Over a String: Take Two (Again) Again, strings are sequences of characters Again, strings are sequences of characters So we can iterate… So we can iterate…Example:myString=“hello” for ch in myString print ch Output:hello hello

The Empty String The empty string is a string that contains nothing The empty string is a string that contains nothing –Represented by two quotation marks (or two single quotes) back-to-back –“” –‘’ Often used in programming for initialization of strings Often used in programming for initialization of strings

String Slicing String slicing creates a new string that consists of a substring of the original string String slicing creates a new string that consists of a substring of the original string<stringName>[start:end] Results in a substring of stringName that begins with the character at index start and ends with the character at index end-1.

String Slicing You can also omit the start and/or end : You can also omit the start and/or end : –If start is omitted, begin at index 0 –If end is omitted, end is equal to len(stringName) What if you omit both?

Example Given a string variable myString that has already been initialized, assign the variable part to contain a string consisting of the first five characters of myString

Question for you: What is the value of s after the following expression is evaluated? myString=“Mary went to school” s=myString[0:4]+myString[-6:] A. Maryschool C. Error B. Mary school

Other Operations: The String Library The string library contains many pre- defined functions that you can use to find out information on a string The string library contains many pre- defined functions that you can use to find out information on a string To use the string library, import it and call using “string”: To use the string library, import it and call using “string”: import string string.find(…) #find is a function

Sample String Library Functions FunctionPurpose capitalize(s)Returns copy of s with the first character capitalized capwords(s)Returns copy of s with first character of each word capitalized center(s, w)Returns copy of s centered in field of width w count(s, str)Returns number of occurrences of str in s find(s,str)Returns index of first occurrence of str in s lower(s)Returns copy of s in all lowercase letters upper(s)Returns copy of s in all uppercase letters split(s)Split s into a list of substrings (splits at blank spaces) strip(s)Returns a copy of s with leading and trailing whitespace characters removed

How to Use String Library Functions Each one returns a value Each one returns a value –You can think of that value as substituting for the function call –Use the function as you would use the value –You’ve seen this before! range() returns a list range() returns a list float() returns a float … float() returns a float …

String Library Functions: Example s=raw_input(“Please enter a string:”) print “string in lower case is: “ + string.lower(s) print “string in upper case is: “ + string.upper(s) print “string with words capitalized is: “ + string.capwords(s)

Question: The string library functions are called like this: string.<functionName>(<args>) What does “ string ” signify? A. The argument is a string B. The function is in the string library C. The return value is a string

Exercise Write a program that reads two strings from the keyboard and indicates whether or not the second string is a substring of the first when case is disregarded. Use a function to determine whether the substring exists, but perform the output in main().

Group Exercise Write a program that reads a word from the keyboard. If the word is not of length 3, print a message indicating the word must be of length 3. Otherwise, determine if the word if a palindrome (i.e., a word that reads the same left to right and right to left).