Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 18 Feb 2011 Strings in Python. Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson.

Similar presentations


Presentation on theme: "Computer Science 18 Feb 2011 Strings in Python. Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson."— Presentation transcript:

1 Computer Science 18 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 so far Strings are generally used to store text data  sample="LEVEL"  Name=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/should not be stored in strings?

4 Strings vs. Integers Both strings and integer variables can store numbers  my_speed_str="300"  my_speed_integer=300 Q: What is the difference between strings and integers?

5 Strings in use Q: Where are strings displayed and entered?

6 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 (including empty).  String are enclosed in quotation marks.  str_var = "300 m/s"  empty_str=""

7 Length of a string Use len(str) to return string length (# of characters)  sample="SERIES"  len(sample)=  empty_str=""  len(empty_str) = 0 6

8 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[ ]  just_v=ss[ ]  Python strings are immutable (can’t change individual chars)  s[0]="B" Try it out 2 0 

9 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“  zeroZero = speed_display[ : ] 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 Slices (sections of a string) 3 1 3 4

10 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 it out: Build a string  build=""  while len(build)<5:  build = build +"a"  print build =40 =“2020”

11 String comparison To test for equality, use the == operator To compare order, use the operators user_name=raw_input("Enter your name- ") if user_name==“Sam": print "Welcome back Sam“ elif user_name<"Sam": print "Your name is before Sam“ else: print "Your name is after Sam“ These operators are case sensitive. Upper case characters are ‘less than’ lower case

12 Operations: Traversing through a string Use a loop to examine each character in a string See HTTLCS for an alternative format: for inHTTLCS strng="count # of us" index=0 count=0 while index < len(strng): if strng[index] == "u“ or strng[index] == “U": count+=1 index+=1 Try it out  How would we traverse backwards?

13 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

14 Advanced Strings

15 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 "Computer Science 18 Feb 2011 Strings in Python. Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson."

Similar presentations


Ads by Google