Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
String Escape Sequences
Printing. printf: formatted printing So far we have just been copying stuff from standard-in, files, pipes, etc to the screen or another file. Say I have.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Variable & Constants. A variable is a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Strings PART II STRING$ AND SPACE$. Create strings of specified number String$ creates string of specified character Space$ creates string of spaces Example:
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Input & Output: Console
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
C-Language Keywords(C99)
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Input, Output, and Processing
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CPS120: Introduction to Computer Science Formatted I/O.
Chapter 5 Strings CSC1310 Fall Strings Stringordered storesrepresents String is an ordered collection of characters that stores and represents text-based.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP C Part III.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
28 Formatted Output.
Formatted Input and Output
Topics Designing a Program Input, Processing, and Output
Data Representation.
Java Variables and Types
ECE Application Programming
ECE Application Programming
Wel come.
TMF1414 Introduction to Programming
OUTPUT STATEMENTS GC 201.
C++ Basics.
Escape Sequences Some Java escape sequences: See Roses.java (page 68)
PYTHON Prof. Muhammad Saeed.
Chapter 3 Mathematical Functions, Strings, and Objects
Chapter 4 INPUT AND OUTPUT OBJECTS
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Topics Designing a Program Input, Processing, and Output
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Strings As well as tuples and ranges, there are two additional important immutable sequences: Bytes (immutable sequences of 8 ones and zeros (usually represented.
Presentation transcript:

Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12 1

Lesson objectives 1. Use common string methods 2. Use escape sequences to represent special characters 3. Use string formatting codes to specify the format of strings for printing and file output 05/02/09 Python Mini-Course: Day 3 – Lesson 12 2

String methods Like everything else in Python, strings are objects Objects have methods that are invoked using dot notation object.method([arguments]) 05/02/09 Python Mini-Course: Day 3 – Lesson 12 3

Examples word = 'banana' new_word = word.upper() print new_word index = word.find('a') print index 05/02/09 Python Mini-Course: Day 3 – Lesson 12 4

String methods For a complete listing, see dtypes.html#string-methods dtypes.html#string-methods 05/02/09 Python Mini-Course: Day 3 – Lesson 12 5

Search methods str.find(sub[, start[, end]]) print word.find('g',2,4) str.index(sub[, start[, end]]) print word.index('g',2,4) str.count(sub[, start[, end]]) print word.count('a') 05/02/09 Python Mini-Course: Day 3 – Lesson 12 6

Formatting methods (return a new string) str.lower() str.upper() str.title() str.swapcase() str.strip([chars]) str.lstrip([chars]) str.rstrip([chars]) 05/02/09 Python Mini-Course: Day 3 – Lesson 12 7

Examples: string_methods2.py s = ' NOBODY expects the Spanish \ Inquisition!!!' print s.lower() print s.upper() print s.title() print s.swapcase() print s.strip() print s.lstrip() print s.rstrip('!') 05/02/09 Python Mini-Course: Day 3 – Lesson 12 8

Format checking methods (return a Boolean value) str.isalnum() str.isalpha() str.isdigit() str.islower() str.isupper() str.istitle() 05/02/09 Python Mini-Course: Day 3 – Lesson 12 9

Splitting and joining* str.split([sep[, maxsplit]]) str.splitlines([keepends]) str.join(seq) *We'll revisit these next week when we cover lists 05/02/09 Python Mini-Course: Day 3 – Lesson 12 10

Special characters To express special characters in Python strings, use Standard C style escape sequences see analysis.html#string-literals analysis.html#string-literals 05/02/09 Python Mini-Course: Day 3 – Lesson 12 11

05/02/09 Python Mini-Course: Day 3 – Lesson Escape SequenceMeaning \\ Backslash (\) \' Single quote (') \" Double quote (") \b ASCII Backspace (BS) \f ASCII Formfeed (FF) \n ASCII Linefeed (LF) \r ASCII Carriage Return (CR) \t ASCII Horizontal Tab (TAB) \uxxxx Character with 16-bit hex value xxxx (Unicode only) \xhh Character with hex value hh (7-bit ASCII)

Example 1: squares.py print 'x\ty' for x in range(1,10): y = x**2 print str(x) + '\t' + str(y) 05/02/09 Python Mini-Course: Day 3 – Lesson 12 13

Example 1: squares.py print 'x\ty\n--\t--' for x in range(1,10): y = x**2 print str(x) + '\t' + str(y) 05/02/09 Python Mini-Course: Day 3 – Lesson 12 14

Example 2: square_roots.py import math print 'x\ty\n--\t--' for x in range(1,10): y = math.sqrt(x) print str(x) + '\t' + str(y) 05/02/09 Python Mini-Course: Day 3 – Lesson 12 15

Formatting string conversions You can use the string formatting operator % to replace values within a string Example: s = 'The knights who say %s' % 'Ni' print s 05/02/09 Python Mini-Course: Day 3 – Lesson 12 16

Formatting string conversions You can do multiple replacements using % within a single string Example: s = 'The %s who say %s' % \ ('Knights', 'Ni') print s 05/02/09 Python Mini-Course: Day 3 – Lesson 12 17

Improving square_roots.py import math print 'x\ty\n--\t--' for x in range(1,10): y = math.sqrt(x) print '%s\t%s' % (x, y) 05/02/09 Python Mini-Course: Day 3 – Lesson 12 18

String formatting codes Syntax %[(name)][flags][width][.precision]code Flags - left-justify + numeric sign (+/-) space blank before positive numbers 0 zero fill 05/02/09 Python Mini-Course: Day 3 – Lesson 12 19

String formatting codes Syntax %[(name)][flags][width][.precision]code Width Number indicating the total field width (max number of characters) Precision Number of digits after the decimal point 05/02/09 Python Mini-Course: Day 3 – Lesson 12 20

05/02/09 Python Mini-Course: Day 3 – Lesson CodeMeaningCod e Meaning s String e Floating-point exponent c Character E e w/ uppercase d Decimal (integer) f Floating-point decimal i Integer F f w/ uppercase u Unsigned (integer) g e or f % Literal '%' G E or F

Examples: integers x = 59 print 'x = %d' % x print 'x = %+d' % x print 'x = %+d%' % x print 'x = %+6d%' % x print 'x = %-6d' % x print 'x = %-6d%' % x print 'x = %06d' % x 05/02/09 Python Mini-Course: Day 3 – Lesson 12 22

Examples: floats x = print 'x = %d' % x print 'x = %f' % x print 'x = %2.4f' % x print 'x = %+2.4f' % x print 'x = %06.2f' % x 05/02/09 Python Mini-Course: Day 3 – Lesson 12 23

Examples: exponential notation x = 1.34e-6 print 'x = %f' % x print 'x = %e' % x print 'x = %g' % x 05/02/09 Python Mini-Course: Day 3 – Lesson 12 24

Example: square_roots2.py import math start, stop = 1, 9 print '%s\t%s' % \ ('x'.center(3), 'y'.center(6)) print '%s\t%s' % (3*'-', 6*'-') for x in range(start, stop+1): y = math.sqrt(x) print '%3d\t%2.4f' % (x, y) 05/02/09 Python Mini-Course: Day 3 – Lesson 12 25