Intro to Strings. CSE 231 Rich Enbody. String: a sequence of characters. Indicated with quotes: or " " 9/11/13

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

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

University of Texas at Arlington, TX, USA

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

CSCA20 Worksheet Strings

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 for Non-Majors

COMP1730/COMP6730 Programming for Scientists. Strings

'...' "..." escaping \u hhhh hhhh '''...''' """...""" raw string Example: r"abc\txyz\n" in code;

Computing with Numbers

Programming in Python 3

Python Intro GIS Week 1. Jake K. Carr

STRINGS. We ve already introduced the string data type a few lectures ago. Strings are subtypes of the sequence data type.

Strings are actually 'objects' Strings

Introduction to Computer Programming for Non-Majors

Spring Semester 11 Exam #1 Dr. Dillon. (02/15)

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

Topic 7: Lists, Dictionaries and Strings

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

Statements 2. a operator= b a = a operator b

The second statement selects character number 1 from and assigns it to.

Python Day 3 11/28/16

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

Python in 10 (50) minutes

CS 106A, Lecture 9 Problem-Solving with Strings

Python: Short Overview and Recap

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

Programming in Python

Spring Semester 10 Exam #1 Dr. Dillon. (02/18)

CS 102 Lab 3 Fall 2012

Typescript on LLVM Language Reference Manual

UTORid: Lecture Section: (circle one): L0101 (MWF10) L0201 (MWF11) Instructor: Jacqueline Smith Jen Campbell

The Practice of Computing Using PYTHON

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

Programming to Python

Introductory Mobile Application Development

Introduction to String Manipulation

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

6. Data Types and Dynamic Typing (Cont.)

A first look at string processing. Python

Script language: Python Data structures

The winning bots both achieved a humanness rating of 52 percent. Human players received an average humanness rating of only 40 percent.

ECS15, Lecture 13. Midterm solutions posted. Topic 4: Programming in Python, cont. Staying up to speed with Python RESOURCES. More Python Resources

String and list processing

Chapter 8: More About Strings. COSC 1436, Summer 2018 Dr. Zhang 7/10/2018

Lehman College, CUNY CMP 230 Exam 2 SAMPLE EXAM Spring 2011

Python Seminar. This is part 2 of 3 Python seminars. Needed Applications Chrome (website: c9.io)

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Copyright 2003 Pearson Education, Inc. Slide 1

CS 106A, Lecture 9 Problem-Solving with Strings

Module 3: Strings and Input/Output

Introduction to: Computers & Programming: Strings and Other Sequences

Chapter 3: Creating and Modifying Text

Instructor s Notes Java - Beginning Built-In Classes. Java - Beginning Built-In Classes

Python Input, output and variables

Variables, Constants, and Data Types

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Babu Madhav Institute of Information Technology, UTU 2015

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

18.1. CS 102 Unit 18. Python. Mark Redekopp

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

[301] Strings. Tyler Caraza-Harter

Object Oriented Programming in Python 3

Strings and Testing string methods, formatting testing approaches CS GMU

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han

CS 1301 Ch 8, Part A

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Chapter 2 Getting Started with Python

PLT Fall Shoo. Language Reference Manual

Control structure: Repetition - Part 1

COLLEGE OF ENGINEERING, NASHIK-4

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

UNIT 5. String Functions and Random Numbers

Python Programming: Lecture 2 Data Types

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

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Python Class-Lesson1 Instructor: Yao

Python Review IPRE

Part III Appendices 165

Chapter 10: Strings and Hashtables

Lecture 3. Strings, Functions, & Modules

Introduction to: Computers & Programming: Strings and Other Sequences

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

Advanced Python. Executive Summary, Session 1

Computing with Strings. Learning Outcomes. Python s String Type 9/23/2012

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

Programming refresher and intro to C programming

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Unit 3. Constants and Expressions

1 Strings (Review) CS151: Problem Solving and Programming

ML 4 A Lexer for OCaml s Type System

a name refers to an object side effect of assigning composite objects

CS 2316 Exam 1 Spring 2013

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

Compound Data Types 1

Student Number: Instructor: Brian Harrington

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Python: common syntax

Semester 2, 2018: Lab 5

Transcription:

CSE 231 Rich Enbody String: a sequence of characters. Indicated with quotes: or " " 2 1

Triple quotes: preserve both the vertical and horizontal formatting of the string. Allows you to type tables, paragraphs, whatever and preserve the formatting: """ this is a test today""" 3 Basic String Operations s = spam length operator len() len(s) 4 + is concatenate new_str = spam + - + spam- print(new_str) spam-spam- * is repeat, the number is how many times new_str * 3 spam-spam-spam-spam-spam-spam- 4 2

in: check if a substring exists in the string. Returns True or False my_str = aabbccdd a in my_str True abb in my_str True x in my_str False aba in my_str False 5 Indexing Strings character index h e l l o w o r l d 0 1 2 3 4 5 6 7 8 9 10-2 -1 every character has an index, starting at 0 the index operator is [ ] my_str = hello world my_str[2] l my_str[-1] d my_str[11] ERROR, index out of range 6 3

Slicing: select a subsequence. Syntax is [start : finish], where: o start is the index that starts the subsequence o finish is the index of one after subsequence end If either start or finish is not provided, start defaults to the beginning of the sequence, finish defaults to the end of the sequence. 7 char index h e l l o w o r l d 0 1 2 3 4 5 6 7 8 9 10 first last my_str = hello world my_str[6:10] worl 8 4

Defaults char index h e l l o w o r l d 0 1 2 3 4 5 6 7 8 9 10 first my_str[6:] world last char index h e l l o w o r l d 0 1 2 3 4 5 6 7 8 9 10 first last my_str[:8] hello wo 9 Slicing also takes three arguments: [start : finish : countby] Default for countby is 1 my_str = hello world my_str[::2] hlowrd 10 5

Python idioms are phrases for a common task. copy a string: my_str = hi mom new_str = my_str[:] reverse a string my_str = madam I m adam reverse_str = my_str[::-1] 11 Indexing S[i] Overview S[0] fetches the first item. Negative indices count backwards from end. S[-2] is second from end 12 6

Slicing S[i:j] Overview Upper bound is not inclusive. Defaults are 0 and end, if omitted Examples o S[1:4] fetches from 1 up to, but not including 4 o S[1:] fetches from 1 to end o S[:4] fetches from beginning up to, but not including 4 o S[:-1] fetches from beginning up to, but not including the last item o S[:] fetches all (A top-level copy of S more later.) 13 Exercise 6-1 7

Strings are immutable: you cannot change one once you create it. but, you can use it to make another string (copy it, slice it, etc.) For example, the reverse string idiom doesn t change the existing string, it simply makes a new string with the letters reversed! 15 Immutable strings Can t change a string once it is created. >>> a_str = spam >>> a_str[1] = l => Error namespace a_str spam 16 8

Immutable strings Can t change a string once it is created. >>> a_str = spam >>> a_str[1] = l => Error You can use it to make new strings >>> x = a_str[:1] + l + a_str[2:] >>> print(a_str, x) spam slam namespace a_str x spam slam 17 Immutable strings Can t change a string once it is created. >>> a_str = spam >>> a_str[1] = l => Error spam You can use it to make new strings >>> x = a_str[:1] + l + a_str[2:] slam >>> print(a_str, x) spam slam scam You can also reassign it >>> a_str = a_str.replace( p, c ) >>> print(a_str) scam Note: nothing refers to spam namespace a_str x 18 9

replace [ ] means it is optional s.replace(old, new [,max]) s = steel y = s.replace( e, o ) print(y) stool print(s) # s did not change steel 19 Method Methods operate on an object. For example, s.replace( l, x ) does a replace on string s (a.k.a. object s) 20 10

Find x = hello x.find( l ) 2 # find index of first l in x Method find operates on the string object x and the two are associated by using the dot notation: x.find( l ). Terminology: the thing(s) in parenthesis, i.e. the l in this case, is called an argument. 21 Split (possibly the most useful string method) s = Always look on the bright side of life. y = s.split() print(y) o ['Always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life.'] print(y[1]) o look 22 11

More methods s.capitalize() s.center(width) s.count(sub,[,start [,end]]) s.ljust(width) s.lower() s.upper() s.lstrip() s.rfind(sub, [,start [,end]]) s.splitlines([keepends]) s.strip() s.translate(table [, delchars]) more at http://docs.python.org/lib/string-methods.html 23 Exercise 6-2 12

Iteration Iteration: another type of repetition. Keyword: for 26 13

for item in sequence: suite 27 for ch in abc : print(ch) 1. First time through, ch = a 2. Second time through, ch = b 3. Third time through, ch = c 4. Nothing left in sequence (string), so quit. 28 14

Iteration (for) in Python is very powerful. 29 range([start], stop,[,step]) for i in range(5): print(i, end = ) 0 1 2 3 4 for i in range(2,7): print(i, end = ) 2 3 4 5 6 30 15

range continued for i in range(1,10,2): print(i, end = ) 1 3 5 7 9 for i in range(10,1,-1): print(i, end = ) 10 9 8 7 6 5 4 3 2 31 for i in range(5): print(i, end = ) 32 16

Exercise 6-3 Palindrome Examples civic, kayak, radar, rotor, level A man, a plan, a canal: Panama. 17

Look at 05_execution_trace (pal1.py) Faster version (pal2.py) Fastest version (pal3.py) 35 String comparisons, single char There are two systems for representing characters: ASCII and Unicode. ASCII takes the English letters, numbers and punctuation marks and associates them with an integer number. Single character comparisons are based on that number. 36 18

Unicode UTF-8 (ASCII subset) Appendix 37 Comparisons within sequence It makes sense to compare within a sequence (lower case, upper case, digits). o a < b True o A < B True o 1 < 9 True Can be weird outside of the sequence o a < A False o a < 0 False 38 19

Whole string comparison Compare the first element of each string o if they are equal, move on to the next character in each string o if they are not equal, the relationship between those to characters are the relationship between the strings o if one ends up being shorter (but equal), the shorter is smaller 39 Examples a < b True aaab < aaac o first difference is at the last char. b < c so aaab is less than aaac. True aa < aaz o The first string is the same but shorter. Thus it is smaller. True 40 20

String formatting print("{} is {} years old".format("bill",25)) Bill is 25 years old 42 21

print("{} is {} years old".format("bill",25)) string to print that contains format information (curly braces) format method object tuple that matches objects to format elements (1 to 1) 43 The format string contains a set of format descriptors that describe how an object is to be printed. format descriptor: :[align][min_width][.precision][descriptor] where [ ] are optional 44 22

descriptor meaning s string d number (decimal) f floating point e floating point (exponent) % floating point as percent 45 Objects are matched in order with format descriptors. print("{} is {} years old".format("bill",25)) Bill is 25 years old 46 23

Width print("{:>10s} is {:<10d} years old".format("bill",25)) string 10 spaces wide including the object right justified (>) number10 spaces wide including the object < means left justified Bill is 25 10 spaces 10 spaces years old this space in the format string already 47 Alignment alignment meaning < left > right = center 48 24

Default alignment "{:10s} is {:10d} years old".format("bill",25) 'Bill is 25 years old Defaults: o String default is left o Number default is right 49 Precision print(math.pi) 3.141592653589793 print("{:.4f}".format(math.pi)) 3.1416 (4 decimal points of precision, with rounding) print("{:10.2f}".format(math.pi)) 3.14 (10 spaces including the number and decimal pt.) 50 25

Position "{}, {}, {}".format('a','b','c ) 'a, b, c "{0}, {1}, {2}".format('a','b','c ) 'a, b, c "{2}, {1}, {0}".format('a','b','c ) 'c, b, a "{2}, {1}, {0}, {1}".format('a','b','c ) 'c, b, a, b' 51 Position, width, precision "{1:4d}, {0:6.2f}".format(math.pi,23) ' 23, 3.14' 52 26

Named '{lat}, {long}'.format(lat='37.24n', long='-27.38w') '37.24N, -27.38W coord = (3,5) 'X: {0[0]}; Y: {0[1]}'.format(coord) 'X: 3; Y: 5' 53 Indexed s = 'Hello t = "there '{0[0]},{1[2]}'.format(s,t) 'H,e' 54 27

Raw Strings allow you to include "\" x = r"c:\python32" print(x) C:\Python32 55 28