Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mr. Fowler Computer Science 14 Feb 2011 Strings in Python.

Similar presentations


Presentation on theme: "Mr. Fowler Computer Science 14 Feb 2011 Strings in Python."— Presentation transcript:

1 Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

2 Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson Objectives  Understand string structure  Access characters and sub-sections from a string  Concatenate strings  Traverse a string

3 Strings in use Strings are generally used to store text data  sample=“LEVEL”  name=raw_input (“What is your name?”) Can also store non-natural language data e.g. telephone numbers  my_tel=“+1 (301) 294 4444”  my_speed=“300 m/s” Q: What is the difference between strings and integers?

4 Strings in use Strings are generally used to store text data  sample=“LEVEL”  name=raw_input (“What is your name?”) Can also store non-natural language data e.g. telephone numbers  my_tel=“+1 (301) 294 4444” Q: What data should (not) be stored in strings?

5 String composition Strings are a composite data type  Built from another data type, characters  In a string, characters are stored in sequence.  Strings can be any length (or empty).  String constants are enclosed in double quotes.  str_var = “300 m/s”  empty_str=“”

6 String length Use len() to return the length of a string  sample=“SERIES”  len(sample)=  empty_str=“”  len(empty_str) =

7 String representation In strings, characters are accessed by index  …like mailboxes in an apartment building.  First index is 0, not 1.  s=“LEVEL”  startChar=s[0]  just_v=s[ ]  Python strings are immutable (can’t change characters).  s[0]=“B” Try it out

8 String subsections Use a range to specify a slice (sub-string)  from start index up to but not including the last index  speed_display = “300 m/s”  middle_two_characters= speed_display[1:3] Omit the first value to select the start of a string  just_num= speed_display[:_] Omit the second value to select the end of a string  just_unit = speed_display[_:] Try it out

9 String operations: Concatenation Combine strings using + (concatenation operator) full_name = “Henry” + “ “ + “James” print “:” + full_name + ”:” Concatenation is not addition  vision_str=“20”+”20”  vision_val=20+20 Try building a string  build=“”  while len(build)<5:  build = build +”a”  print build

10 String comparison To test for equality, use the == operator user_name=raw_input(“Enter your username?”) if user_name==“Brad”: print “Welcome back Bradley” To compare order, use the operators if user_name<“Sam”: print “Your name is before Sam’s in the phone book” These operators are case sensitive. Upper case characters are ‘less than’ lower case Try it out

11 Traversing through a string Use a loop to examine each character in a string strng=“count the number of u’s in this string” index = 0 count=0 while index < len(strng): if strng[index] == “u” count+=1 How would we traverse backwards? Try it out See HTTLCS for an alternative format: for in

12 Summary Strings are composed of characters len(sample) :String length sample[i] :Character at index i (starts at 0) 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 More details and exercises: HTLLCS Ch 7HTLLCS Ch 7

13 Advanced Strings

14 String operations: find find() searches for a string within a string To use it, insert this at the top of your code:  import string find() returns the first index of a substring  full_name = “Henry James”  string.find(full_name,”e”) You can specify the starting point of the search: string.find(full_name,”e”,2) If the string is not found, find() returns -1 find() is case sensitive


Download ppt "Mr. Fowler Computer Science 14 Feb 2011 Strings in Python."

Similar presentations


Ads by Google