Strings and Lists – the split method

Slides:



Advertisements
Similar presentations
Python for Informatics: Exploring Information
Advertisements

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Python regular expressions. “Some people, when confronted with a problem, think ‘I know, I'll use regular expressions.’ Now they have two problems.”
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Java Strings in 10 minutes
Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
Programming Seminar 2009 Night 1. Review: Last Time We covered variables, for loops, functions, modules, and how programming isn’t math – Actually, we.
Recursion Introduction to Computing Science and Programming I.
Introduction to Python. Outline Python IS ……. History Installation Data Structures Flow of Control Functions Modules References.
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
Files in Python Caution about readlines vs. read and split.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
The Scanner Class and Formatting Output Mr. Lupoli.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Sets Day 1 Part II.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
PYTHON EEG ANALYSIS EXAMPLE FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Python for NLP Regular Expressions CS1573: AI Application Development, Spring 2003 (modified from Steven Bird’s notes)
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can.
More Strings CS303E: Elements of Computers and Programming.
Strings The Basics. Strings a collection data type can refer to a string variable as one variable or as many different components (characters) string.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions.
Sets Part II.
The Scripting Programming Language
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
Type Casting. Type casting Sometimes you need a piece of data converted to a different type In Python you do a typecast to cause that to happen int(3.599)
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.
Find LCM Least Common Multiple of 3 and 5: List the Multiples of each number, The multiples of 3 are 3, 6, 9, 12, 15, 18,... etc The multiples of 5 are.
Chapter 4 Computing With Strings Charles Severance Textbook: Python Programming: An Introduction to Computer Science, John Zelle.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Main Points: - Working with strings. - Problem Solving.
Introduction to Computing Science and Programming I
Computer Programming Fundamentals
Python - Lists.
Chapter 4 Computing With Strings
Python regular expressions
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 09 – Strings
Strings in Python Creating a string.
String Manipulation.
Strings Part 2 Taken from notes by Dr. Neil Moore
Advanced String handling
Lists in Python.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
The compareTo interface
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists in Python Creating lists.
Strings and the slice operator
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders All assignments are now posted.
Division Properties of Exponents
Intro to Computer Science CS1510 Dr. Sarah Diesburg
By Himanshi dixit 11th ’B’
Division Properties of Exponents
CSE 231 Lab 6.
Python regular expressions
Strings Part 2 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Introduction to Computer Science
Presentation transcript:

Strings and Lists – the split method

The split method for strings This method is very powerful and is unique to Python It only applies to strings – remember this! The syntax is source.split(delimiter) or source.split() What does it do? it breaks the string into smaller pieces based on the delimiter string (if given) or using any whitespace character (if no delimiter given) It returns a list of strings

With no delimiters Suppose that str is “ abc \t def\t xyz “ str has lots of whitespace in it If you give the expression str.split() you get the result [“abc”, “def”, “xyz”] Note that the elements of the list do NOT have any whitespace in them at all Note that something like “ \t “ is used as one delimiter, not used individually (which would give you lots of empty strings in the list)

With delimiters Suppose str contained “abbabbadefbb” and you wrote the expression str.split(“bb”) You get the result [“a”,”a”,”adef”,””] Note that there is an empty string at the end of the list because the delimiter was found at the very end of the string (same thing happens if the delimiter is at the start of the string also) You are only allowed one string as a delimiter, it can be as long as you wish