Introduction to Python Strings in Python Strings.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
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”?
Introduction to Python
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.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented 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?
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
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.
Walk through previous lecture. TSP Questions: How many tours are there? How do you solve it? How fast can you solve it?
Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.
Python for Informatics: Exploring Information
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
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.
Strings in Python. Computers store text as strings GATTACA >>> s = "GATTACA" s Each of these are characters.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible via index offsets into its set of elements. Examples:
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
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.
Introduction to Python Sajal Desai. What is Python? Versitile, simple, high level language No linking and compilation “By the way, the language is named.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
Computer Science 18 Feb 2011 Strings in Python. Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson.
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
Introduction to Python
String class.
CS 100: Roadmap to Computing
Presented By S.Yamuna AP/IT
Python - Functions.
Variables, Expressions, and Statements
Numbers, lists and tuples
Conditionals (if-then-else)
Topics Introduction to File Input and Output
CHAPTER THREE Sequences.
Python - Strings.
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Basic String Operations
Python for Informatics: Exploring Information
Strings in Python.
CHAPTER 3: String And Numeric Data In Python
CS1110 Today: collections.
Python Basics with Jupyter Notebook
Introduction to Computer Science
Lists and the ‘for’ loop
“Everything Else”.
Topics Introduction to File Input and Output
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Python Strings in Python Strings

Components of Python 1) Variables (Me hold data) 2) Data-type (integers, float, ) 3) Data-structures (list, tuple, dicitonary) 4) Loop (for, while) 5) Branch (if, elif, else) 6) Methods 7) Functions

Computers store text as strings >>> s = "GATTACA" 1 2 3 4 5 6 s G A T C 's' is variable! Each of these are characters

Why are strings important? Sequences are strings ..catgaaggaa ccacagccca gagcaccaag ggctatccat.. Database records contain strings LOCUS AC005138 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

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

1 2 3 4 5 6 G A T C Getting substrings >>> s[1:3] 'AT' 'ACA' >>> s[3:5] 'TA' >>> s[:] 'GATTACA' >>> s[::2] 'GTAA' >>> s[-2:2:-1] 'CAT' >>> 1 2 3 4 5 6 G A T C

Creating strings Strings start and end with a single or double quote characters (they must be the same) "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.’

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 ") \n Newline \t Tab

Working with strings length concatenation repeat substring test >>> len("GATTACA") 7 >>> "GAT" + "TACA" 'GATTACA' >>> "A" * 10 'AAAAAAAAAA' >>> "G" in "GATTACA" True >>> "GAT" in "GATTACA" >>> "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 "<stdin>", line 1, in ? TypeError: cannot concatenate 'str' and 'int' objects >>> int("38") + 5 43 >>> "38" + str(5) '385' >>> int("38"), str(5) (38, '5') >>> int("2.71828") ValueError: invalid literal for int(): 2.71828 >>> float("2.71828") 2.71828 >>>

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 "<stdin>", 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 >>>

Ask the user for a sequence then print its length 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 8 bases long adenine: 1 thymine: 3 cytosine: 1 guanine: 1 unknown: 2