Strings in Python. Computers store text as strings GATTACA 0123456 >>> s = "GATTACA" s Each of these are characters.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
Computing with Strings CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
January 24, 2006And Now For Something Completely Different 1 “And Now For Something Completely Different” A Quick Tutorial of the Python Programming Language.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Bell Ringer What types are numbers are there is the python programming language?
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Python for Informatics: Exploring Information
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
COMPSCI 101 Principles of Programming Lecture 4 –The type() function, the string type, the len() function, string slices.
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.
Lists and the ‘ for ’ loop. Lists Lists are an ordered collection of objects >>> data = [] >>> print data [] >>> data.append("Hello!") >>> print data.
DNA Bases. Adenine: Adenine: (A) pairs with Thymine (T) only.
Outline  Why should we use Python?  How to start the interpreter on a Mac?  Working with Strings.  Receiving parameters from the command line.  Receiving.
Functions Chapter 4 Python for Informatics: Exploring Information
CS346 Javascript -3 Module 3 JavaScript Variables.
Chapter 5 Strings CSC1310 Fall Strings Stringordered storesrepresents String is an ordered collection of characters that stores and represents text-based.
Python Conditionals chapter 5
ITEC 109 Lecture 7 Operations. Review Variables / Methods Functions Assignments –Purpose? –What provides the data? –What stores the data? –What type of.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible via index offsets into its set of elements. Examples:
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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
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.
SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible via index offsets into its set of elements. Examples:
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.
LECTURE 5 Strings. STRINGS We’ve already introduced the string data type a few lectures ago. Strings are subtypes of the sequence data type. Strings are.
High-level Programming with Python Expressions and Variables Alex Ropelewski PSC-NRBSC Bienvenido Vélez UPR Mayaguez Reference: How to Think Like a Computer.
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.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
Input, Output and Variables GCSE Computer Science – Python.
CSC 231: Introduction to Data Structures Dr. Curry Guinn.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
CSc 120 Introduction to Computer Programing II Adapted from slides by
String class.
CS 100: Roadmap to Computing
String Processing Upsorn Praphamontripong CS 1110
Presented By S.Yamuna AP/IT
Python - Functions.
Learning to Program in Python
Python - Strings.
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Data types Numeric types Sequence types float int bool list str
Basic String Operations
Strings in Python.
CHAPTER 3: String And Numeric Data In Python
CS1110 Today: collections.
15-110: Principles of Computing
Topics Basic String Operations String Slicing
Introduction to Python Strings in Python Strings.
String methods 26-Apr-19.
Lists and the ‘for’ loop
Unit 3: Variables in Java
“Everything Else”.
Topics Basic String Operations String Slicing
Topics Basic String Operations String Slicing
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Strings in Python

Computers store text as strings GATTACA >>> s = "GATTACA" s Each of these are characters

Why are strings important? Sequences are strings..catgaaggaa ccacagccca gagcaccaag ggctatccat.. Database records contain strings LOCUS AC DEFINITION Homo sapiens chromosome 17, clone hRPK.261_A_13, complete sequence AUTHORS Birren,B., Fasman,K., Linton,L., Nusbaum,C. and Lander,E. HTML is one (big) string

Getting Characters GATTACA >>> s = "GATTACA" >>> s[0] 'G' >>> s[1] 'A' >>> s[-1] 'A' >>> s[-2] 'C' >>> s[7] Traceback (most recent call last): File " ", line 1, in ? IndexError: string index out of range >>>

Getting substrings GATTACA >>> s[1:3] 'AT' >>> s[:3] 'GAT' >>> s[4:] 'ACA' >>> s[3:5] 'TA' >>> s[:] 'GATTACA' >>> s[::2] 'GTAA' >>> s[-2:2:-1] 'CAT' >>>

Creating strings "This is a string" "This is another string" "" "Strings can be in double quotes" ‘Or in single quotes.’ 'There’s no difference.' ‘Okay, there\’s a small one.’ Strings start and end with a single or double quote characters (they must be the same)

Special Characters and Escape Sequences Backslashes (\) are used to introduce special characters >>> s = 'Okay, there\'s a small one.' The \ “escapes” the following single quote >>> print s Okay, there's a small one.

Some special characters Escape Sequence Meaning \\Backslash (keep a \) \'Single quote (keeps the ') \"Double quote (keeps the ") \nNewline \tTab

Working with strings >>> len("GATTACA") 7 >>> "GAT" + "TACA" 'GATTACA' >>> "A" * 10 'AAAAAAAAAA' >>> "G" in "GATTACA" True >>> "GAT" in "GATTACA" True >>> "AGT" in "GATTACA" False >>> "GATTACA".find("ATT") 1 >>> "GATTACA".count("T") 2 >>> length concatenation repeat substring test substring location substring count

Converting from/to strings >>> "38" + 5 Traceback (most recent call last): File " ", line 1, in ? TypeError: cannot concatenate 'str' and 'int' objects >>> int("38") >>> "38" + str(5) '385' >>> int("38"), str(5) (38, '5') >>> int(" ") Traceback (most recent call last): File " ", line 1, in ? ValueError: invalid literal for int(): >>> float(" ") >>>

Change a string? Strings cannot be modified They are immutable Instead, create a new one >>> s = "GATTACA" >>> s[3] = "C" Traceback (most recent call last): File " ", line 1, in ? TypeError: object doesn't support item assignment >>> s = s[:3] + "C" + s[4:] >>> s 'GATCACA' >>>

Some more methods >>> "GATTACA".lower() 'gattaca' >>> "gattaca".upper() 'GATTACA' >>> "GATTACA".replace("G", "U") 'UATTACA' >>> "GATTACA".replace("C", "U") 'GATTAUA' >>> "GATTACA".replace("AT", "**") 'G**TACA' >>> "GATTACA".startswith("G") True >>> "GATTACA".startswith("g") False >>>

Ask for a string The Python function “raw_input” asks the user (that’s you!) for a string >>> seq = raw_input("Enter a DNA sequence: ") Enter a DNA sequence: ATGTATTGCATATCGT >>> seq.count("A") 4 >>> print "There are", seq.count("T"), "thymines" There are 7 thymines >>> "ATA" in seq True >>> substr = raw_input("Enter a subsequence to find: ") Enter a subsequence to find: GCA >>> substr in seq True >>>

Assignment 1 Ask the user for a sequence then print its length Enter a sequence: ATTAC It is 5 bases long

Assignment 2 Modify the program so it also prints the number of A, T, C, and G characters in the sequence Enter a sequence: ATTAC It is 5 bases long adenine: 2 thymine: 2 cytosine: 1 guanine: 0

Assignment 3 Modify the program to allow both lower-case and upper-case characters in the sequence Enter a sequence: ATTgtc It is 6 bases long adenine: 1 thymine: 3 cytosine: 1 guanine: 1

Assignment 4 Modify the program to print the number of unknown characters in the sequence Enter a sequence: ATTU*gtc It is 7 bases long adenine: 1 thymine: 3 cytosine: 1 guanine: 1 unknown: 2