Python - Strings.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

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.
1 Python Chapter 3 Reading strings and printing. © Samuel Marateck.
Chapter 2 Writing Simple Programs
Strings. Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.
Python for Informatics: Exploring Information
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
Functions Chapter 4 Python for Informatics: Exploring Information
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
More Strings CS303E: Elements of Computers and Programming.
File I/O CMSC 201. Overview Today we’ll be going over: String methods File I/O.
Notes on Python Regular Expressions and parser generators (by D. Parson) These are the Python supplements to the author’s slides for Chapter 1 and Section.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Python - 2 Jim Eng Overview Lists Dictionaries Try... except Methods and Functions Classes and Objects Midterm Review.
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.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
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.
Chapter 4 Computing With Strings Charles Severance Textbook: Python Programming: An Introduction to Computer Science, John Zelle.
Python Basics.
Chapter 2 Writing Simple Programs
String and Lists Dr. José M. Reyes Álamo.
Strings Chapter 6 Python for Everybody
CSc 120 Introduction to Computer Programing II Adapted from slides by
Introduction to Python
Notes on Python Regular Expressions and parser generators (by D
Module 4 String and list – String traversal and comparison with examples, List operation with example, Tuples and Dictionaries-Operations and examples.
Python - Lists.
String Processing Upsorn Praphamontripong CS 1110
Functions Chapter 4 Python for Everybody
CMPT 120 Topic: Python strings.
Presented By S.Yamuna AP/IT
Chapter 4 Computing With Strings
Python - Functions.
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
Topics Introduction to File Input and Output
Chapter 8 More on Strings and Special Methods
Chapter 8 More on Strings and Special Methods
Data types Numeric types Sequence types float int bool list str
Strings.
Chapter 8 More on Strings and Special Methods
CSC 221: Introduction to Programming Fall 2018
Python for Informatics: Exploring Information
String and Lists Dr. José M. Reyes Álamo.
Basic String Operations
Python for Informatics: Exploring Information
CS 1111 Introduction to Programming Spring 2019
15-110: Principles of Computing
Topics Basic String Operations String Slicing
Introduction to Computer Science
Introduction to Computer Science
Topics Basic String Operations String Slicing
Topics Introduction to File Input and Output
Class code for pythonroom.com cchsp2cs
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CMPT 120 Topic: Python strings.
Topics Basic String Operations String Slicing
Presentation transcript:

Python - Strings

String Data Type >>> str1 = "Hello” >>> str2 = 'there' >>> bob = str1 + str2 >>> print bob Hellothere >>> str3 = '123' >>> str3 = str3 + 1 Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: cannot concatenate 'str' and 'int' objects >>> x = int(str3) + 1 >>> print x 124 >>> -- 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 -- Convert numbers in a string into a number using int()

Reading and Converting >>> name = raw_input('Enter:') Enter:Chuck >>> print name Chuck >>> apple = raw_input('Enter:') Enter:100 >>> x = apple – 10 Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for -: 'str' and 'int' >>> x = int(apple) – 10 >>> print x 90 -- Read data using strings and then parse and convert the data as needed -- This gives more control over error situations and/or bad user input -- Raw input numbers must be converted from strings

Looking Inside Strings b a n a n a 0 1 2 3 4 5 >>> fruit = 'banana' >>> letter = fruit[1] >>> print letter a >>> n = 3 >>> w = fruit[n - 1] >>> print w n -- 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 computed

A Character Too Far >>> zot = 'abc' >>> print zot[5] Traceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: string index out of range >>> -- You will get a python error if you attempt to index beyond the end of a string. -- Be careful when constructing index values and slices

Strings Have Length b a n a n a 0 1 2 3 4 5 0 1 2 3 4 5 >>> fruit = 'banana' >>> print len(fruit) 6 -- There is a built-in function len, which gives the length of a string

Len Function len() function >>> fruit = 'banana' >>> x = len(fruit) >>> print x 6 A function is some stored code. A function takes some input and produces an output. len() function 'banana' (a string) 6 (a number) Guido wrote this code

Len Function >>> fruit = 'banana' >>> x = len(fruit) >>> print x 6 A function is some stored code. A function takes some input and produces an output. def len(inp): blah blah for x in y: blah blah 'banana' (a string) 6 (a number)

Looping Through Strings fruit = 'banana' 0 b index = 0 1 a while index < len(fruit) : 2 n letter = fruit[index] 3 a print index, letter 4 n index = index + 1 5 a -- Using a while statement and an iteration variable, and the len function, a loop can be constructed 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 b a n a n a fruit = 'banana' for letter in fruit : print letter

Looping Through Strings fruit = 'banana' for letter in fruit : print letter -- A definite loop using a for statement is much more elegant -- The iteration variable is completely taken care of by the for loop b a n a n a index = 0 while index < len(fruit) : letter = fruit[index] print letter index = index + 1

Looping and Counting word = 'banana' count = 0 for letter in word : if letter == 'a' : count = count + 1 print count -- This is a simple loop, which loops through each letter in a string and counts the number of times the loop encounters the 'a' character.

Looking deeper into in for letter in 'banana' : print letter -- The iteration variable “iterates” though 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 Six-character string Iteration variable for letter in 'banana' : print letter

Done? Advance letter b a n a n a Yes print letter for letter in 'banana' : print letter The iteration variable “iterates” though the string and the block (body) of code is executed once for each value in the sequence

Slicing Strings M o n t y P y t h o n 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 -- Determine any continuous section of a string using a colon operator >>> s = 'Monty Python' >>> print s[0:4] Mont >>> print s[6:7] P >>> print s[6:20] Python -- 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 the string, it stops at the end Slicing Strings

Slicing Strings M o n t y P y t h o n 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 >>> s = 'Monty Python' >>> print s[:2] Mo >>> print s[8:] Thon >>> print s[:] Monty Python -- If the first number or the last number of the slice is missing, it is assumed to be the beginning or end of the string respectively Slicing Strings

String Concatenation >>> a = 'Hello' >>> b = a + 'There' >>> print b HelloThere >>> c = a + ' ' + 'There' >>> print c Hello There >>> -- When the + operator is applied to strings, it means "concatenation"

Using in as an Operator >>> fruit = 'banana’ >>> 'n' in fruit True >>> 'm' in fruit False >>> 'nan' in fruit True >>> if 'a' in fruit : ... print 'Found it!’ ... -- The in keyword can also be used to check if one string is "in" another string -- The in expression is a logical expression and returns True or False and can be used in an if statement Found it! >>>

String Comparison if word == 'banana': print 'All right, bananas.' print 'Your word,' + word + ', comes before banana.’ elif word > 'banana': print 'Your word,' + word + ', comes after banana.’ else: print 'All right, bananas.'

String Library -- Python has a number of string functions, which are in the string library -- These functions are already built into every string - 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 >>> greet = 'Hello Bob‘ >>> zap = greet.lower() >>> print zap hello bob >>> print greet Hello Bob >>> print 'Hi There'.lower() hi there

>>> stuff = 'Hello world’ >>> type(stuff) <type 'str'> >>> dir(stuff) ['capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] http://docs.python.org/lib/string-methods.html

http://docs.python.org/lib/string-methods.html

Searching a String b a n a n a 0 1 2 3 4 5 0 1 2 3 4 5 >>> fruit = 'banana' >>> pos = fruit.find('na') >>> print pos 2 >>> aa = fruit.find('z') >>> print aa -1 -- 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 a string position starts at zero

Making everything UPPER CASE >>> greet = 'Hello Bob' >>> nnn = greet.upper() >>> print nnn HELLO BOB >>> www = greet.lower() >>> print www hello bob >>> -- Copy 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 >>> greet = 'Hello Bob' “search and replace” operation in >>> nstr = greet.replace('Bob','Jane') >>> print nstr Hello Jane >>> nstr = greet.replace('o','X') >>> print nstr HellX BXb >>> a word processor -- It replaces all occurrences of the search string with the replacement string

Stripping Whitespace -- A whitespace can be removed from a string at the beginning and/or end -- lstrip() and rstrip() to the left and right only -- strip() Removes both begin and ending whitespace >>> greet = ' Hello Bob ' >>> greet.lstrip() 'Hello Bob ' >>> greet.rstrip() ' Hello Bob' >>> greet.strip() 'Hello Bob' >>>

Prefixes >>> line = ‘this is string example…wow!!’ >>> line.startswith(‘this') True >>> line.startswith(‘is‘, 2,4) True >>> line.startswith(‘this‘, 2,4) False