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

Similar documents
Introduction to String Manipulation

CS 115 Lecture 8. Selection: the if statement. Neil Moore

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

CS 115 Lecture 4. More Python; testing software. Neil Moore

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

UNIT 5. String Functions and Random Numbers

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

Computing with Numbers

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

Working with Sequences: Section 8.1 and 8.2. Bonita Sharif

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

String Processing CS 1111 Introduction to Programming Fall 2018

CMSC201 Computer Science I for Majors

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

CS Summer 2013

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CS 115 Lecture 21. Classes, data structures, and C++ Neil Moore

SSOL Language Reference Manual

Advanced Python. Executive Summary, Session 1

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

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

Python Intro GIS Week 1. Jake K. Carr

CS1004: Intro to CS in Java, Spring 2005

18.1. CS 102 Unit 18. Python. Mark Redekopp

CS 231 Data Structures and Algorithms, Fall 2016

Python for loops. Girls Programming Network School of Information Technologies University of Sydney. Mini-lecture 7

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

A first look at string processing. Python

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

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

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

ISE 101 Introduction to Information Systems. Lecture 3 Objectives: While loops Strings

Chapter 10: Creating and Modifying Text Lists Modules

printf( Please enter another number: ); scanf( %d, &num2);

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

Babu Madhav Institute of Information Technology, UTU 2015

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

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

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Strings are actually 'objects' Strings

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

Python Day 3 11/28/16

Unit 2. Srinidhi H Asst Professor

Introduction to Python. Data Structures

CSCA20 Worksheet Strings

Introduction to Python

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

Chapter 5 BET TER ARRAYS AND STRINGS HANDLING

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

UNIVERSITÀ DI PADOVA. < 2014 March >

Exercise: The basics - variables and types

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Python: common syntax

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CMSC201 Computer Science I for Majors

Introduction to Computer Programming for Non-Majors

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

An Introduction to Python

Programming in Python

Python for Everybody. Exploring Data Using Python 3. Charles R. Severance

Java Bytecode (binary file)

Announcements COMP 141. Accessing Characters Review. Using len function 10/30/2017. Strings II. Reminders: Program 6 - due 11/2

Full file at

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Text Input and Conditionals

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

CS1 Lecture 9 Feb. 5, 2018

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

Problem Solving for Intro to Computer Science

CSC148 Fall 2017 Ramp Up Session Reference

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS102: Variables and Expressions

Compound Data Types 1

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

Chapter 2 Basic Elements of C++

Types, Expressions, and States

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Strings, Lists, and Sequences

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Python Class-Lesson1 Instructor: Yao

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Fall 2017 CISC124 9/16/2017

MULTIPLE CHOICE. Chapter Seven

Lecture Transcript While and Do While Statements in C++

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

DM536 Programming A. Peter Schneider-Kamp.

Program Planning, Data Comparisons, Strings

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

Python Review IPRE

CMSC 201 Spring 2016 Lab 04 For Loops

Programming with Java

Transcription:

CS 115 Lecture 13 Strings Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 29 October 2015

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() Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 2 / 16

String in detail Let s see how to do more things with strings: Find the length. Get individual characters. Extract ranges of characters ( slicing ). Convert to uppercase/lowercase. Search for letters or substrings. Search and replace substrings. Remove whitespace. Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 3 / 16

String length The length of a string is the number of characters in it. Spaces count! So do newlines and other special characters. To get the length of a string, use the len function: name = "HAL 9000" numchars = len(name) # 8 Argument type: string Return type: integer What is len("")? Zero. We ll see later that len works with lists too. Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 4 / 16

Extracting characters The characters in a string are numbered from 0 to length 1 HAL 9000 (length = 8) 01234567 This number is called the position or index of the character. You can use square brackets to get the character at a given position. first = name[0] # "H" This is called subscripting or indexing. The position must be smaller than the length: print(name[8]) # ERROR: out of range You can subscript with negative numbers, to count from the end. name[-1] is the last character (rightmost). name[-2] is the next-to-last character.... name[-len(name)] is the first character. name[-i] is like name[len(name) - i] name[-9] would still be out of range! Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 5 / 16

Extracting whole 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. Subscript using a slice ( slicing ). Syntax: start, a colon :, and stop (one-past-the-end). Similar semantics to range(start, stop). The first three characters: name[0:3] # "HAL" Start at character 0, stop before character 3. Two through four: name[2:5] # "L 9" Can leave out either part: Leave out start: start at the beginning. first = name[:3] # "HAL" Leave out stop: go until the end. last = name[4:] # "9000" The whole thing (kind of silly here): copy = name[:] # "HAL 9000" Doesn t change the original string! (Makes a new one). Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 6 / 16

Converting case Python strings have several methods to change their case (capitalization). These methods don t change the original string, either. They return a new string, so use them with assignment. All lowercase: lazy = name.lower() albert einstein All uppercase: telegraph = name.upper() ALBERT EINSTEIN First letter uppercase: almost = name.capitalize() Albert einstein First letter of each word uppercase: good = name.title() Gives Albert Einstein One use: case-insensitive comparison. For example, a yes-no prompt: The user might type Y or y or N or n. Convert the input to uppercase and compare that! if userin.upper() == "Y": # handles "y" too Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 7 / 16

Searching inside a string Python has two ways to search inside a string for a substring. The in operator: needle in haystack needle and haystack are both strings (for now). Returns a boolean. if " " in name: # if name contains a space The substring can occur anywhere: beginning, middle, or end. if "CS" in class: # CS115, SCSI, 1CS Case-sensitive! if "cs" in "CS115": # FALSE! It must be contiguous: if "C1" in "CS115": # FALSE! Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 8 / 16

Searching inside a string Sometimes you need to know now 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 = beginning, etc). Returns -1 if it was not found. Add another argument to start searching in the middle: pos = haystack.find(needle, 4) # start at position 4 In a loop you can use the last match + 1: sp1 = haystack.find(" ") # first space sp2 = haystack.find(" ", sp1 + 1) # next space rfind is similar, but searches backwards. So finds the last occurrence. text = "the last space here" lastsp = text.rfind(" ") # 14 To reverse-search from the middle, give the beginning and end: prevsp = text.rfind(" ", 0, lastsp) # 8 Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 9 / 16

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] # before the space last = name[space + 1 : ] # after the space Here s a loop to find all the words in a string: text = "a string with many words" prevspace = -1 nextspace = text.find(" ", prevspace + 1) while nextspace!= -1: word = text[prevspace + 1 : nextspace] print("word: ", word) prevspace = nextspace nextspace = text.find(" ", prevspace + 1) print("last word: ", text[prevspace + 1 : ]) words.py Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 10 / 16

Search and replace Often you don t really care where the substrings are, but just want to replace them with something else. Use the replace method. newstr = str.replace("from", "to") Finds all the occurrences of from and replaces them with to. Doesn t modify the original: returns a new string. You can tell replace to only replace the first few occurrences. course = "CS 115 introduction to programming" print(course.replace(" ", "-", 1)) # first occurrence "CS-115 introduction to programming" Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 11 / 16

Strip When getting input from the 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, etc... Does not affect whitespace in the middle! Does not change the original string: returns a new one. userin = " \tcs 115 \n" clean = userin.strip() # "CS 115" Can strip from only the left or right with lstrip and rstrip: lclean = userin.lstrip() # "CS 115 \n" rclean = userin.rstrip() # " \tcs 115" print(userin) # What does this print? Original doesn t change! " \tcs 115 \n" Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 12 / 16

Traversing strings The for loop in Python can iterate not just over a range of integers, but also over the characters of 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 each iteration of the loop, char will be one character. In order. Not a number! So if name is "Hal": The first time, char = "H" Then, char = "a" Finally, char = "l" Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 13 / 16

String traversal examples Let s write a couple of programs using strings and for loops to: 1 Check if a string contains a digit. How is this different from string.isdigit()? Because that checks if all the characters are digits. hasdigit.py 2 Remove the vowels from a string. Remember, we can t modify the original string. So we ll need to build a new string for the result. We ll assign to this new string to append the letters we want. The string will be a kind of accumulator! devowel.py Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 14 / 16

Iterating with an index Traversing a string gives you the characters, but not their positions. If I m traversing HAL 9000, the body of the loop has no way to know which 0 it s currently looking at. That s fine for many uses, but sometimes you do care. There are three ways to do this: 1 Loop over the string and keep a counter. Initialize the counter to zero (start at the beginning). Increment the counter at the end of each iteration. 2 Loop over the range of indices (plural of index ): for i in range(len(name)): Inside the loop, name[i] gives the character at that index. 3 Use enumerate to get both at the same time. for i, char in enumerate(name): Each iteration, i will be the index... and char the character at that index. Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 15 / 16

Iterating with an index Let s change our hasdigit function to finddigit in three ways. 1 finddigit-counter.py 2 finddigit-range.py 3 finddigit-enumerate.py Neil Moore (UK CS) CS 115 Lecture 13 Fall 2015 16 / 16