STRINGS IN PYTHON. Where have we seen strings before? #string type variables s = “hello” #obtaining keyboard input from the user choice = input(“Please.

Slides:



Advertisements
Similar presentations
Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009.
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.
Intro to Python Paul Martin. History Designed by Guido van Rossum Goal: “Combine remarkable power with very clear syntax” Very popular in science labs.
Guide to Programming with Python
PHP : Hypertext Preprocessor
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
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.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
Python Programming Chapter 7: Strings Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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 CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Programming in Python Part III Dr. Fatma Cemile Serçe Atılım University
Creating a Database Designing Structure, Capturing and Presenting Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
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:
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
More Strings CS303E: Elements of Computers and Programming.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
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.
Mr. Fowler Computer Science 14 Feb 2011 Strings in Python.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
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.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
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.
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.
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.
Chapter Four Common facilities of procedural languages.
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.
Introduction to Python Lesson 2a Print and Types.
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.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Introduction to Programming
Python Basics.
String and Lists Dr. José M. Reyes Álamo.
String Manipulation Part 1
Input and Output Upsorn Praphamontripong CS 1110
CSc 120 Introduction to Computer Programing II Adapted from slides by
Tuples and Lists.
Basic operators - strings
Creation, Traversal, Insertion and Removal
Chapter 8 More on Strings and Special Methods
4. sequence data type Rocky K. C. Chang 16 September 2018
Chapter 8 More on Strings and Special Methods
Introduction to Programming
4.1 Strings ASCII & Processing Strings with the Functions
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Fundamentals of Python: First Programs
Basic String Operations
Python Basics with Jupyter Notebook
Topics Basic String Operations String Slicing
Introduction to Computer Science
Introduction to Computer Science
Topics Basic String Operations String Slicing
Data Types and Maths Programming Guides.
Topics Basic String Operations String Slicing
COMPUTING.
Class 8 setCoords Oval move Line cloning vs copying getMouse, getKey
Presentation transcript:

STRINGS IN PYTHON

Where have we seen strings before? #string type variables s = “hello” #obtaining keyboard input from the user choice = input(“Please enter your name”) #printing text out to the screen print(s, choice)

Where do we use strings? Everywhere!

Strings vs. Integers Both strings and integer variables can store data my_speed_str = “300” my_speed_integer = 300 How does the computer treat strings and integers differently?

What are strings made of?! Strings are a composite data type Composed of smaller elements called characters Characters are stored in sequence Strings can be any length (including empty). long_str = “here is a really long string...blah!” empty_str = “”

String representation and indexing Each character is located in different slots (like lockers) Each character in a string has an index Indexing starts at 0! s = “hello world!” Access individual slots with the [ ] print(s[0]) print(s[6]) hell owo rld!

String indexing practice s = “Mr. Paul Bui” print(s[4], s[9]) #Try it out with your name #Print out your initials using indexing

String length Use len(str) to get the string length (# of characters) sample = “hello world!” len(sample)= ??? empty_str = “” len(empty_str) = ??? #Print out the length of your full name

Slices (sections of a string) Think of a slice of pizza (it can be any size) Use a range to specify a slice (substring) string[ start : end ] Includes start index up to but not including the end index Example: s = “Mr. Paul Bui” print(s[4:8]) #What would be the slice for “Bui”? #Try slicing just your first name

More slicing Omit the first value to select the start of a string s = “the quick brown fox” print(s[ : 10 ]) #What do we see? Omit the second value to select the end of a string s = “the quick brown fox” print(s[ 10 : ]) #What do we see? Try it out

String Concatenation Concatenation means connecting Combine strings using + (concatenation operator) firstName = “John” lastName = “Doe” fullName = firstName + “ ” + lastName

String comparison Comparisons: ==,, >= Pretend that letters and words are on a number line A-Z, a-Z a == b checks if the strings are the same (equal) To compare order, use the operators Upper case characters are ‘less than’ lower case

String comparison example name = input("Enter your name: ") if name == “Bui”: print “Welcome back Bui“ elif name < “Bui”: print “Your name is before Bui” else: print “Your name is after Bui”

Traversing through a string Use a loop to examine each character in a string s = “Grumpy wizards make toxic brew for the evil Queen and Jack” x = 0 while x < len(s): print(s[x]) x += 1 Try it out How would we traverse backwards?

String concatenation in a loop build = “” x = 0 while x < 5: build = build + “a” x += 1 print(build)

Summary Strings are composed of characters len(sample) String length sample[i] Character at index i sample[start:end] Slice from start up to but not including end index sample+sample Concatenate strings sample=="test" Test for equality sample<test Test for order