Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.

Slides:



Advertisements
Similar presentations
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Advertisements

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.
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.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Guide to Programming with Python
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
I Power Higher Computing Software Development High Level Language Constructs.
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:
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
More Strings CS303E: Elements of Computers and Programming.
1 STRINGS String data type Basic operations on strings String functions String procedures.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
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.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
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.
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.
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.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions.
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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Computer Science 18 Feb 2011 Strings in Python. Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
String and Lists Dr. José M. Reyes Álamo.
CSc 120 Introduction to Computer Programing II Adapted from slides by
CMSC201 Computer Science I for Majors Lecture 14 – Tuples
String Processing Upsorn Praphamontripong CS 1110
CSc 120 Introduction to Computer Programing II
Lesson 07: Strings Topic: Introduction to Programming, Zybook Ch 6, P4E Ch 6. Slides on website.
Introduction to Strings
Introduction to Strings
Chapter 7: Strings and Characters
Perl Variables: Array Web Programming.
CHAPTER THREE Sequences.
10.1 Character Testing.
Chapter (3) - Looping Questions.
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Basic String Operations
Introduction to Strings
15-110: Principles of Computing
Topics Basic String Operations String Slicing
Introduction to Computer Science
Introduction to Strings
CMSC201 Computer Science I for Majors Lecture 16 – Tuples
Topics Basic String Operations String Slicing
By Himanshi dixit 11th ’B’
Introduction to Strings
REPETITION Why Repetition?
Topics Basic String Operations String Slicing
COMPUTING.
Presentation transcript:

Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence of one character strings; includes the empty string "" Operators: +concatenation – join 2 strings *repetition – repeat string x times Examples: "cat"+" "+"dog"  "cat dog" ("---"+"|")*3  "---|---|---|"

Strings … are a sequence Since strings are a sequence and since for loops iterate over a sequence, for item in sequence: strings and for loops work well together. name = "Laurier" # create string for char in name: # char is not a keyword print ("{}".format(char)) for loops are ideal when you need to look at every character in a string

Write a function to remove spaces in a phrase. e.g. “some random words ” #main phrase_orig = input("Enter phrase: ") phrase_nosp= remove_sp (phrase_orig) print(phrase_nosp)

Strings … items have an index A sequence is ordered by position access items by their position or index name = "Laurier " u = 'Wilfrid Laurier University' print("{}{}{}{}".format(u[0], u[1], u[2], u[3])) print("{}{}{}{}".format(u[-1], u[-2], u[-3], u[-4]))

Strings … slicing Slicing is a general form of indexing that extracts a slice (section) in one step. =>typical form is x[start:limit] ≡ everything in x starting at index start up to but not including index limit univ = 'Wilfrid Laurier University ' print("{}".format(univ[8:15])) print("{}".format(univ[-11:-1])) print("{}".format(univ[-11:len(univ)]))

Strings … slicing  general form is x[start:limit:increment] ≡ everything in x starting at index start up to but not including index limit in steps of increment where: start – defaults to 0 limit – defaults to len(x) increment – defaults to 1 if start is not less than limit, return a null string, "" univ = 'Wilfrid Laurier University ' e.g. univ[:5] univ[:] univ[::3]

Strings …immutability Strings are immutable: they cannot be changed in place, or you cannot overwrite the values in a string name = 'Carol' name[0] = 'B'<= causes error #'str' object does not support item assignment but you can create a new string object name = 'Carol' name = 'B' + name[1:]

Write a function to parse a name into first and last names. #main full_name = input("Enter name ") first, last = parse_name (full_name) print(first) print(last)

Write a program to check the area code of a phone number. # main num = input ("Enter phone number with area code ") acode = input ("Enter area code ") ok = area_check(num, acode)

Common String Operations OperationResult compare operations: =, > Compares string1 operator string2 -> comparison based on ASCII value -> equivalent to dictionary order for numbers and letters x in string x not in string True if x is equal to item in string; ** False if x is equal to item in string item1 + item2concatenation item * nconcatenate n copies of item string[ i ]item i of string, numbering starts at 0 string[ i:j ] string[ i:j:k ] slice of string from i to j slice of string from i to j with step k len(string)length of string ** Can be used for substring testing, e.g. “gl” in “glow”.