1 / 16

Strings in Python

Strings in Python. Where have we seen strings before?. #string type variables s = “ hello” #obtaining keyboard input from the user choice = input(“ Please enter your name”) #printing text out to the screen print(s, choice). Where do we use strings? . Everywhere! http://www.google.com

liseli
Download Presentation

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

  2. Where have we seen strings before? #string type variables s = “hello” #obtaining keyboard input from the user choice = input(“Please enter your name”) #printing text out to the screen print(s, choice)

  3. Where do we use strings? • Everywhere! • http://www.google.com • http://www.wikipedia.com • http://www.paulbui.net/wl/IB_Computer_Science_1

  4. Strings vs. Integers • Both strings and integer variables can store data • my_speed_str = “300” • my_speed_integer = 300 • How does the computer treat strings and integers differently?

  5. What are strings made of?! • Strings are a composite data type • Composed of smaller elements called characters • Characters arestored in sequence • Strings can be any length (including empty). • long_str = “here is a really long string...blah!” • empty_str = “”

  6. String representation and indexing • Each character is located in different slots (like lockers) • Each character in a string has an index • Indexing starts at 0! s = “hello world!” • Access individual slots with the [ ] print(s[0]) print(s[6]) h e l l o w o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11

  7. String indexing practice s = “Mr. Paul Bui” print(s[4], s[9]) #Try it out with your name #Print out your initials using indexing

  8. String length • Use len(str) to get the string length (# of characters) sample = “hello world!” len(sample)= ??? empty_str = “” len(empty_str) = ??? #Print out the length of your full name

  9. Slices (sections of a string) • Think of a slice of pizza (it can be any size) • Use a range to specify a slice (substring) string[ start : end ] • Includes start index up to but not including the end index • Example: s = “Mr. Paul Bui” print(s[4:8]) #What would be the slice for “Bui”? #Try slicing just your first name

  10. More slicing • Omit the first value to select the start of a string s = “the quick brown fox” print(s[ : 10 ]) #What do we see? • Omit the second value to select the end of a string s = “the quick brown fox” print(s[ 10 : ]) #What do we see? • Try it out

  11. String Concatenation • Concatenation means connecting • Combine strings using + (concatenation operator) firstName = “John” lastName = “Doe” fullName = firstName + “ ” + lastName

  12. String comparison • Comparisons: ==, <, <=, >, >= • Pretend that letters and words are on a number line • A-Z, a-Z • a == b checks if the strings are the same (equal) • To compare order, use the < and > operators • Upper case characters are ‘less than’ lower case

  13. String comparison example name = input("Enter your name: ") if name == “Bui”: print “Welcome back Bui“ elif name < “Bui”: print “Your name is before Bui” else: print “Your name is after Bui”

  14. Traversing through a string • Use a loop to examine each character in a string s = “Grumpy wizards make toxic brew for the evil Queen and Jack” x = 0 while x < len(s): print(s[x]) x += 1 • Try it out • How would we traverse backwards?

  15. String concatenation in a loop build = “”x = 0 while x < 5: build = build + “a” x += 1 print(build)

  16. 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

More Related