Introduction to Computer Science

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

Lists Introduction to Computing Science and Programming I.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 2 Writing Simple Programs
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Python for Informatics: Exploring Information
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Fundamentals of Python: First Programs
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
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.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
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.
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
Chapter 4 Computing With Strings Charles Severance Textbook: Python Programming: An Introduction to Computer Science, John Zelle.
Chapter 2 Writing Simple Programs
String and Lists Dr. José M. Reyes Álamo.
COMPUTATIONAL CONSTRUCTS
Strings Chapter 6 Python for Everybody
© 2016 Pearson Education, Ltd. All rights reserved.
CS 100: Roadmap to Computing
CMPT 120 Topic: Python strings.
Chapter 4 Computing With Strings
Strings Part 1 Taken from notes by Dr. Neil Moore
Strings Chapter 6 Slightly modified by Recep Kaya Göktaş in April Python for Informatics: Exploring Information
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Introduction to Strings
Introduction to Strings
Chapter 7: Strings and Characters
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Chapter 8 JavaScript: Control Statements, Part 2
Loop Structures and Booleans Zelle - Chapter 8
Chapter 8 More on Strings and Special Methods
Chapter 8 More on Strings and Special Methods
Python - Strings.
Strings.
4. sequence data type Rocky K. C. Chang 16 September 2018
CSC 221: Introduction to Programming Fall 2018
Python for Informatics: Exploring Information
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Basic String Operations
Introduction to Strings
15-110: Principles of Computing
Topics Introduction to File Input and Output
Topics Basic String Operations String Slicing
Bryan Burlingame 13 March 2019
Introduction to Strings
Introduction to Computer Science
Topics Basic String Operations String Slicing
Topics Introduction to File Input and Output
Class code for pythonroom.com cchsp2cs
Introduction to Strings
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
Introduction to Computer Science
Introduction to Computer Science
Presentation transcript:

Introduction to Computer Science Programming with Python

Data Structures - Strings Chapter 6

String Data Type A string is a sequence of characters A string literal uses quotes ‘Hello’ or “Hello” For strings, + means concatenate When a string contains numbers, it is still a string We can convert numbers in a string into a number (integer/float) using int()

Reading and Converting We prefer to read data in using strings and then parse and convert the data as we need This gives us more control over error situations and/or bad user input Input numbers must be converted from strings

Looking Inside Strings We can get any single character in a string using an index specified in square brackets The index value must be an integer and starts at zero The index value can be an expression that is computer

A Character Too Far You will get a Python error if you attend to index beyond the end of a string So, be careful when constructing index values and slices

Length of a String The built-in function len gives us the length of a string

Len Function A function is some stored code that we use. A function takes some input and produce an output

Looping Through Strings Using a while statement and an iteration variable, and the len function, we can construct a loop to look at each of the letters in a string individually

Looping Through Strings A definite loop using a for statement is much more elegant The iteration variable is completely taken care of by the for loop

Looping Through Strings A definite loop using a for statement is much more elegant The iteration variable is completely taken care of by the for loop This is the Pythonic way. Translate that to classic for loops that are common in other/most programming languages

Looping and Counting This is a simple loop that loops through each letter in a string and counts the number of times the encounters the ‘a’ character

Looking Deeper into in The iteration variable iterates through the sequence (ordered set) The block (body) of code is executed once for each value in the sequence The iteration variable moves through all of the values in the sequence

Looping through a string – for loop The iteration variable iterates through the string and the block (body) of code is executed once for each value in the sequence

Slicing Strings It is possible to look at any continuous section of a string using a colon separator Example, [ from : to] The second number is one beyond the end of the slice up to but not including If the second number is beyond the end of string, it stops at the end

Slicing Strings If we leave off the first number or the last number of the slice, it is assumed to be the beginning or end of the string respectively

String Concatenation When the + operator is applied to strings, it means concatenation

Using in as a Logical Operator The in keyword can also be used to check to see if one string is “in” another string The in expressions is a logical expression that returns True or False and can be used in an if statement

String Comparison

String Comparison A < a z < a

String Library Python has a number of string functions which are in the string library These functions are already built into every string. We invoke them by appending the function to the string variable These functions do not modify the original string, instead they return a new string that has been altered

https://docs.python.org/3/library/stdtypes.html#string-methods

String Library

Searching a String We use the find() function to search for a substring within another string find() finds the first occurrence of the substring If the substring is not found, find() returns -1 Remember that string position starts at zero

Making Everything UPPER CASE You can make a copy of a string in lower case or upper case Often when we are searching for a string using find() we first convert the string to lower case so we can search a string regardless of case

Search and Replace The replace() function is like a search and replace operation in a word processor It replaces all occurrences of the search string with the replacement string

Stripping Whitespace Sometimes we want to take a string and remove whitespace at the beginning and/or end lstrip() and rstrip() remove whitespaces at the left or right strip() removes both beginning and ending whitespace

Prefixes

Parsing and Extracting From Stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 21 31

Strings and Character Set In Python3, all strings are Unicode

Summary String type Read/Convert Indexing [] Slicing string [2:4] Looping through string with for and while Concatenating string with + String operations String library String Comparisons Searching in strings Replacing text Stripping white space