Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Container Types in Python
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.
Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction to Python
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Input, Output, and Processing
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
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.
Strings CS303E: Elements of Computers and Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Simple Python Data Victor Norman CS104 Calvin College.
Variables, Expressions, and Statements
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter 5 Strings CSC1310 Fall Strings Stringordered storesrepresents String is an ordered collection of characters that stores and represents text-based.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Strings in Python. Computers store text as strings GATTACA >>> s = "GATTACA" s Each of these are characters.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Strings See Chapter 2 u Review constants u Strings, concatenation and repetition 1.
Lists Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Strings The Basics. Strings a collection data type can refer to a string variable as one variable or as many different components (characters) string.
Tuples Chapter 10 Python for Informatics: Exploring Information
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
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.
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.
Topics Designing a Program Input, Processing, and Output
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
Introduction to Python
CHAPTER THREE Sequences.
Bryan Burlingame 17 October 2018
Chapter 8 More on Strings and Special Methods
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Data types Numeric types Sequence types float int bool list str
Introduction to Primitive Data types
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists in Python Outputting lists.
Topics Designing a Program Input, Processing, and Output
Strings in Python.
Topics Designing a Program Input, Processing, and Output
Nate Brunelle Today: Strings, Type Casting
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
Topics Designing a Program Input, Processing, and Output
Bryan Burlingame 13 March 2019
Introduction to Python Strings in Python Strings.
Topics Basic String Operations String Slicing
By Himanshi dixit 11th ’B’
CSE 231 Lab 7.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Primitive Data types
Topics Basic String Operations String Slicing
Lists basics A container is a construct used to group related values together and contains references to other objects instead of data. A list is a container.
Introduction to Python
Presentation transcript:

Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Python

Strings Strings are sequences of characters

PythonStrings Strings are sequences of characters No separate character type: just a string of length 1

PythonStrings Strings are sequences of characters No separate character type: just a string of length 1 Indexed exactly like lists

PythonStrings Strings are sequences of characters No separate character type: just a string of length 1 Indexed exactly like lists name = 'Darwin' print name[0], name[-1] D n

PythonStrings for iterates through characters

PythonStrings for iterates through characters name = 'Darwin' for c in name: print c D a r w i n

PythonStrings Use either ' or " (as long as they match)

PythonStrings Use either ' or " (as long as they match) print 'Alan', "Turing" Alan Turing

PythonStrings Use either ' or " (as long as they match) print 'Alan', "Turing" Alan Turing Strings are the same no matter how they're created

PythonStrings Use either ' or " (as long as they match) print 'Alan', "Turing" Alan Turing Strings are the same no matter how they're created print 'Alan' == "Alan" True

PythonStrings Strings are compared character by character from left to right

PythonStrings Strings are compared character by character from left to right print 'a' < 'b' True

PythonStrings Strings are compared character by character from left to right print 'a' < 'b' True print 'ab' < 'abc' True

PythonStrings Strings are compared character by character from left to right print 'a' < 'b' True print 'ab' < 'abc' True print '1' < '9' True

PythonStrings Strings are compared character by character from left to right print 'a' < 'b' True print 'ab' < 'abc' True print '1' < '9' True print '100' < '9' True

PythonStrings Strings are compared character by character from left to right print 'a' < 'b' True print 'ab' < 'abc' True print '1' < '9' True print '100' < '9' True print 'A' < 'a' True

PythonStrings Strings are immutable : cannot be changed in place

PythonStrings Strings are immutable : cannot be changed in place name = 'Darwin' name[0] = 'C' TypeError: 'str' object does not support item assignment

PythonStrings Strings are immutable : cannot be changed in place name = 'Darwin' name[0] = 'C' TypeError: 'str' object does not support item assignment Immutability improves performance

PythonStrings Strings are immutable : cannot be changed in place name = 'Darwin' name[0] = 'C' TypeError: 'str' object does not support item assignment Immutability improves performance See later how immutability improves programmers' performance

PythonStrings Use + to concatenate strings

PythonStrings Use + to concatenate strings name = 'Charles' + ' ' + 'Darwin' print name Charles Darwin

PythonStrings Use + to concatenate strings name = 'Charles' + ' ' + 'Darwin' print name Charles Darwin Concatenation always produces a new string

PythonStrings Use + to concatenate strings name = 'Charles' + ' ' + 'Darwin' print name Charles Darwin Concatenation always produces a new string 'Charles' original original = 'Charles'

PythonStrings Use + to concatenate strings name = 'Charles' + ' ' + 'Darwin' print name Charles Darwin Concatenation always produces a new string 'Charles' name original original = 'Charles' name = original

PythonStrings Use + to concatenate strings name = 'Charles' + ' ' + 'Darwin' print name Charles Darwin Concatenation always produces a new string original = 'Charles' name = original name += ' Darwin' 'Charles' 'Charles Darwin' name original

PythonStrings Often used to format output

PythonStrings Often used to format output print 'reagant: ' + str(reagant_id) + ' produced ' + \ str(percentage_yield) + '% yield'

PythonStrings Often used to format output print 'reagant: ' + str(reagant_id) + ' produced ' + \ str(percentage_yield) + '% yield' There's a better way...

PythonStrings Use string % value to format output

PythonStrings Use string % value to format output output = 'reagant: %d' % 123 print output reagant: 123

PythonStrings Use string % value to format output output = 'reagant: %d' % 123 print output reagant: 123 percentage_yield = 12.3 print 'yield: %6.2f' % percentage_yield yield: 12.30

PythonStrings And string % (v1, v2,...) for multiple values

PythonStrings And string % (v1, v2,...) for multiple values reagant_id = 123 percentage_yield = 12.3 print 'reagant: %d produced %f% yield' % \ (reagant_id, percentage_yield) reagant: 123 produced 12.30% yield

PythonStrings And string % (v1, v2,...) for multiple values reagant_id = 123 percentage_yield = 12.3 print 'reagant: %d produced %f% yield' % \ (reagant_id, percentage_yield) reagant: 123 produced 12.30% yield % operator turns double '%' into single '%'

PythonStrings Use \n to represent a newline character

PythonStrings Use \n to represent a newline character Use \' for single quote, \" for double quote

PythonStrings Use \n to represent a newline character Use \' for single quote, \" for double quote print 'There isn\'t time\nto do it right.' There isn't time to do it right.

PythonStrings Use \n to represent a newline character Use \' for single quote, \" for double quote print 'There isn\'t time\nto do it right.' There isn't time to do it right. print "But you said,\n\"There is time to do it over.\"" But you said, "There is time to do it over."

PythonStrings Use \\ for a literal \ character

PythonStrings print 'Most mathematicians write a\\b instead of a%b.' Most mathematicians write a\b instead of a%b. Use \\ for a literal \ character

PythonStrings print 'Most mathematicians write a\\b instead of a%b.' Most mathematicians write a\b instead of a%b. Use \\ for a literal \ character Common pattern with escape sequences

PythonStrings print 'Most mathematicians write a\\b instead of a%b.' Most mathematicians write a\b instead of a%b. Use \\ for a literal \ character Common pattern with escape sequences –Use a character to mean "what follows is special"

PythonStrings print 'Most mathematicians write a\\b instead of a%b.' Most mathematicians write a\b instead of a%b. Use \\ for a literal \ character Common pattern with escape sequences –Use a character to mean "what follows is special" –Double it up to mean "that character itself"

PythonStrings Use triple quotes (either kind) for multi-line strings

PythonStrings Use triple quotes (either kind) for multi-line strings quote = '''We can only see a short distance ahead, but we can see plenty there that needs to be done.'''

PythonStrings quote = '''We can only see a short distance ahead, but we can see plenty there that needs to be done.''' d,\nbu Use triple quotes (either kind) for multi-line strings

PythonStrings quote = '''We can only see a short distance ahead, but we can see plenty there that needs to be done.''' quote = 'We can only see\na short distance ahead\n' + \ 'but we can see plenty there\nthat needs to be done.' Use triple quotes (either kind) for multi-line strings

PythonStrings Strings have methods

PythonStrings Strings have methods name = 'newTON' print name.capitalize(), name.upper(), name.lower(), name Newton NEWTON newton newTON

PythonStrings Strings have methods name = 'newTON' print name.capitalize(), name.upper(), name.lower(), name Newton NEWTON newton newTON dna = 'acggtggtcac' print dna.count('g'), dna.count('x') 4 0

PythonStrings Strings have methods name = 'newTON' print name.capitalize(), name.upper(), name.lower(), name Newton NEWTON newton newTON dna = 'acggtggtcac' print dna.count('g'), dna.count('x') 4 0 print dna.find('t'), dna.find('t', 5), dna.find('x')

PythonStrings Strings have methods name = 'newTON' print name.capitalize(), name.upper(), name.lower(), name Newton NEWTON newton newTON dna = 'acggtggtcac' print dna.count('g'), dna.count('x') 4 0 print dna.find('t'), dna.find('t', 5), dna.find('x') print dna.replace('t', 'x'), dna acggxggxcac acggtggtcac

PythonStrings Strings have methods name = 'newTON' print name.capitalize(), name.upper(), name.lower(), name Newton NEWTON newton newTON dna = 'acggtggtcac' print dna.count('g'), dna.count('x') 4 0 print dna.find('t'), dna.find('t', 5), dna.find('x') print dna.replace('t', 'x') acggxggxcac acggtggtcac print dna.replace('gt', '') acggcac

PythonStrings Can chain method calls together

PythonStrings Can chain method calls together element = 'cesium' print element.upper().center(10, '.')

PythonStrings Can chain method calls together element = 'cesium' print element.upper().center(10, '.') convert to upper case

PythonStrings Can chain method calls together element = 'cesium' print element.upper().center(10, '.') center in a field 10 characters wide

PythonStrings Can chain method calls together element = 'cesium' print element.upper().center(10, '.')..CESIUM..

October 2010 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. narrated by Dominique Vuvan