Chapter 10: Creating and Modifying Text Lists Modules

Similar documents
Strings are actually 'objects' Strings

CS 1124 Media computation. Lecture 9.1, October 20, 2008 Steve Harrison

Introduction to: Computers & Programming: Strings and Other Sequences

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

CMPT 125: Lecture 3 Data and Expressions

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Basic data types. Building blocks of computation

Variables, Constants, and Data Types

Full file at

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

DECODE SPECIAL OPERATOR, FORMAT OPERATOR CONTENTS TRIPLE QUOTES. IS a-to-z METHODS REPLACE L E N G T H E X P A N D T A B S ENC0D3

A variable is a name for a location in memory A variable must be declared

Sequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.

Java Basic Datatypees

3 The Building Blocks: Data Types, Literals, and Variables

Getting started with Java

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

CS-201 Introduction to Programming with Java

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Introduction to: Computers & Programming: Strings and Other Sequences

Script language: Python Data structures

"Hello" " This " + "is String " + "concatenation"

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Chapter 3: Creating and Modifying Text

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Variables and Values

Part III Appendices 165

The Practice of Computing Using PYTHON. Chapter 4. Working with Strings. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Programming in Python 3

Full file at

UNIVERSITÀ DI PADOVA. < 2014 March >

Formatted Output Pearson Education, Inc. All rights reserved.

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

COMP Primitive and Class Types. Yi Hong May 14, 2015

MATHEMATICAL FUNCTIONS CHARACTERS, AND STRINGS. INTRODUCTION IB DP Computer science Standard Level ICS3U

Basics of Java Programming

Python Tutorial. Day 1

CS 115 Lecture 13. Strings. Neil Moore. Department of Computer Science University of Kentucky Lexington, Kentucky

Chapter 2: Data and Expressions

Program Elements -- Introduction

TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

Chapter 2.4: Common facilities of procedural languages

Python Class-Lesson1 Instructor: Yao

Chapter 2 Working with Data Types and Operators

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

CS102: Variables and Expressions

Lecture 2 Tao Wang 1

Strings. Upsorn Praphamontripong. Note: for reference when we practice loop. We ll discuss Strings in detail after Spring break

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Strings, Strings and characters, String class methods. JAVA Standard Edition

Chapter 17. Fundamental Concepts Expressed in JavaScript

UNIT 5. String Functions and Random Numbers

String Processing CS 1111 Introduction to Programming Fall 2018

AP Computer Science A

Semester 2, 2018: Lab 5

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Program Planning, Data Comparisons, Strings

Chapter 10: Creating and Modifying Text

Using Java Classes Fall 2018 Margaret Reid-Miller

4 Programming Fundamentals. Introduction to Programming 1 1

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

Chapter 2: Data and Expressions

Table of Contents Date(s) Title/Topic Page #s. Abstraction

B.V. Patel Institute of BMC & IT, UTU 2014

2 nd Week Lecture Notes

Advanced Computer Programming

Computing with Numbers

Visual C# Instructor s Manual Table of Contents

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Lecture 3. Input, Output and Data Types

Introduction to String Manipulation

Babu Madhav Institute of Information Technology, UTU 2015

COMP1730/COMP6730 Programming for Scientists. Strings

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

We now start exploring some key elements of the Java programming language and ways of performing I/O

RTL Reference 1. JVM. 2. Lexical Conventions

Chapter 2: Data and Expressions

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

C: How to Program. Week /Mar/05

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Eng. Mohammed Abdualal

Advanced Algorithms and Computational Models (module A)

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017

Introduction to TURING

Introduction to Programming, Aug-Dec 2006

String Computation Program

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Transcription:

Chapter 10: Creating and Modifying Text Lists Modules

Text Text is manipulated as strings A string is a sequence of characters, stored in memory as an array H e l l o 0 1 2 3 4

Strings Strings are defined with quote marks. Python supports three kinds of quotes: Single quotes: Double quotes: Triple quotes: You must start and end a string with the same type of quote Example >>> print 'this is a string' this is a string >>> print "this is a string" this is a string >>> print """this is a string""" this is a string

Nes-ng quotes Quotes can be nested It makes it easy to put quotes inside strings Use the right one that allows you to embed quote marks you want Example: >>> asinglequote = " ' " >>> print asinglequote ' >>> print Muhammad Ali once said Don't count the days, make the days count

What is a triple quote, anyway? A triple quote allows us to embed new lines, spaces, tabs, etc. in our strings. In general, it is more useful when used in the program area Example: tripleq()

For loops and Strings Since a string is an array of characters, we can use the for loop to get access to each individual character Example: for eachcharacter in mystring : print eachcharacter In this case eachcharacter serves as the index for each element (character ) in the array Example: thecharacters()

Encodings for strings Strings are just arrays of characters In most cases, characters are represented with a single byte. The ASCII encoding standard maps between single byte values and the corresponding characters http://asciiset.com/ Example: thecode() More recently, characters are represented with two bytes. Unicode uses two bytes per characters so that there are encodings for glyphs (characters) of other languages Java uses Unicode. The version of Python we are using is based in Java, so our strings are actually using Unicode.

There are more characters than we can type Our keyboards don t have all the characters available to us, and it s hard to type others into strings. For example: Backspace Return و We use escape sequences or backslash escapes to get other characters in to strings We put together a backslash followed by a character to represent these other symbols

Backslash escapes \b is backspace \n is a newline (to represent pressing the Enter key) \t is a tab \uxxxx is a Unicode character, where XXXX is a code: Each X can be a digit (0-9) or a letter between A F. http://www.unicode.org/charts/ Must precede the string with u for Unicode to work Example: escape()

Remember the r Remember the r we were using in front of the path to a file? pic = makepicture (r C:\bio101\tree.jpg ) The r in this case tells Python not to use a escape sequence whenever it finds a backslash followed by a character inside a string - - - instead it should use the string in raw mode In this example note that we have \b and \t inside the string but we don t mean backspace or tab in this case. NOTE: this is for Windows machines only

Manipula-ng strings We can get the length of a string (how many characters are in the string) with the len() function. >>> text = hello there >>> print len(text) >>> 11 The + operator when used with strings performs a concatenation operation: appends the second string at the end of the first >>> s = cat >>> t = fish >>> print s + t catfish

Manipula-ng strings The * operator when used with strings performs a repeat operation, where the resulting string has as many copies of the original string as indicated by the integer. Example: >>> s = $ >>> t = s * 5 >>> print t $$$$$

The str() and int() func-ons The str() function converts a number into a string Example: num = 99 num2 = 10.55 strnum = str(num) #strnum is 99 strnum2 = str(num2) #strnum2 is 10.55 The int() function can be used to convert a string into an integer number Example: ticket = 101 number = int(ticket) #number = 101

The float() func-on The float() function can be used to convert a string into an floating- point number Example: average = 10.56 number = float(average) #number = 10.56 The float() function can also be used force an integer value into a float Example: n = float (101) #n becomes 101.0

GeFng parts of strings We use the subscript operator [] to get parts of strings. string[n] gives you the n th character in the string string[n:m] gives you the n th up to (but not including) the m th character. string[:m] n is assumed to be zero. string[n:] m is assumed to be the end of the string Negative numbers can be used at either end to trim off that much from that side. Example: subscript()

Very important to remember The index of the first character is always zero The index of the last character is always the lenght minus 1 H e l l o 0 1 2 3 4 Here size is 5 First character H at index 0 Last character 0 at index 4

Dot nota-on All data in Python are actually objects Objects combine data and methods that act on the object. Methods are special functions that only objects of the same type understand. Methods are functions known only to certain objects To execute a method, you use dot notation Object.method() Examples: color = pixel.getcolor() len = mysound.getlength()

Useful string methods capitalize(): returns a capitalized string à the first character of the string is made into an uppercase letter. It does not alter the original string startswith(prefix): returns true if the string starts with the given prefix endswith(suffix): returns true if the string ends with the given suffix find(mystr) and find(mystr,start) and find (mystr,start,end) finds mystr in the string and returns the index number where mystr starts. You can tell it what index number to start the search from, and even where to stop looking. It returns - 1 if it fails.

Useful string methods rfind(mystr), rfind(mystr,start) and rfind (mystr,start,end) similar to find but searches from the back of the string toward the front. Returns the index number where mystr starts. You can tell it what index number to start the search from, and even where to stop looking. It returns - 1 if it fails. upper() returns a string where all the characters are upper case. It does not alter the original string. lower() returns a string where all the characters are lower case. It does not alter the original string.

Useful string methods swapcase() returns a copy of the original string where all uppercase letters have been swapped to lowercase and vice versa. It does not alter the original string. title() returns a string where just the first character is uppercase and the rest lowercase. It does not alter the original string. isalpha() returns true if the string is not empty and all letters. Otherwise it returns false. isdigit() returns true if the string is not empty and all numbers. Otherwise it returns false. replace(search, replace) searches for the search string and replaces it with the replace string. It returns the result but does not change the original string.

Lists We ve seen lists before that s what range() returns. Lists contains a collection of values Lists can contain strings, numbers, even other lists. List can contain a mix of types They work very much like strings You get elements out with [] You can add lists together (using the + sign) You can use for loops on them We can use them to process a variety of types of data. To define a list, use the [] and separate the elements with commas

Lists and access to elements We use the subscript operator [] to access elements in a list >>> print (list[2]) #this will access the element at index 2 [m:n] will return all the elements between m and n, not including the element at n >>> print (list[2:5]) [m:] will return all the elements from m until the end of the list >>> print (list[2:]) [:n] will return all the elements from the beginning until n, not including the element at n >>> print (list[:5]) Negative numbers can be used at either end to trim off that much from that side. >>> print list[1:-1]

More on lists Examples: >>> names = [ Ana, Bob, Claire ] >>> grades = [100, 98, 88, 75] >>> mixedlist = [ Jane, Bennet, 25, 45000.50] >>> phonelist = [ Ana, [ home, 1234567], [ mobile, 9876543], [ work, 2348765, [ extension, 3456]]] Remember that the first element of a list is at index zero. >>> student = names[0] #that would be Ana The + sign allows us to append one list at the end of another >>> newlist = names + grades >>> print newlist >>> [ Ana, Bob, Claire, 100, 98, 88, 75] Using the subscript operator >>> print grades[1:3] #will print 98 88

The in operator You can use the in operator to determine whether and item is contained in a list. Format: item in list This expression returns true if item is found in the list, or false otherwise Example: fruits = [ banana, cherry, pear, apple ] search = pear if search in fruits : else : print (search, was found in fruits ) print (search, was not found in fruits )

Methods to use with lists append(element) adds element at the end of the list. remove(element) removes element from the list, if it s there. sort() puts the list in alphabetical/ascending order reverse() reverses the list count(element) tells you the number of times that element appears in the list. It returns an integer. split(delimiter) converts a string into a list of sub- strings, using delimiter to separate the strings. It returns a list of strings. Note: This is a string method that returns a list

Some useful list func-ons max() takes a list as input and returns the maximum value in the list. min() takes a list as input and returns minimum value in the list. len() takes a list as an input and returns the number of elements in that list (the size of the list) as an integer

Adding new capabili-es: Modules In every programming language there is a way of extending the basic capabilities of the language by adding new functionality In Python we do this by importing external modules. A module is a Python file with a bunch of additional functions and objects defined within it. By importing the module, we make the module s capabilities available to our program.

Python s Standard Library Python has an extensive library of modules that come with it. The Python standard library includes modules that allow us to : access the Internet, deal with time, generate random numbers, access files in a directory, and lots, lots more.

Accessing features of a module We access the additional capabilities of a module using dot notation, after we import the module: import modulename #this line imports a module modulename.feature #use a feature of modulename How do you know what modules and features are there? Check the documentation. Python comes with a Library Guide. http://docs.python.org/py3k/library/ There are books like Python Standard Library by Fredrik Lundh, that describe the modules and provide examples.

Module documenta-on Python Standard Library by Fredrik Lundh

The random module This module has functions for generating random numbers and making random choices. Here are three of the functions in this module: random.random() generates random numbers between 0 and 1. It returns a floating point number. random.randint(low, high) generates random integer numbers between low and high (inclusive) random.choice(stringlist) picks random words from a list. It takes a list of strings as input

Many other Python Standard Libraries datetime and calendar know how to manipulate dates, times, and calendars. math knows lots of important mathematical functions zipfile knows how to make and read.zip files email lets you build your own spam program, or filter spam, or build an email tool for yourself. SimpleHTTPServer is a complete working Web server.