1 / 15

Strings in Python

Strings in Python. Computer Science 18 Feb 2011. 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.

wyome
Télécharger la présentation

Strings in Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings in Python Computer Science 18 Feb 2011

  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 arestored 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) = 6 0

  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 0 2 

  9. Slices (sections of a string) • 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 1 3 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 =“2020” =40

  11. String comparison • To test for equality, use the == operator • To compare order, use the < and > 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 HTTLCSfor an alternative format: for in 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

More Related