Presentation is loading. Please wait.

Presentation is loading. Please wait.

STRINGS IN PYTHON. Where have we seen strings before? #string type variables s = “hello” #obtaining keyboard input from the user choice = input(“Please.

Similar presentations


Presentation on theme: "STRINGS IN PYTHON. Where have we seen strings before? #string type variables s = “hello” #obtaining keyboard input from the user choice = input(“Please."— 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 are stored 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]) hell owo rld! 0123 4567891011

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


Download ppt "STRINGS IN PYTHON. Where have we seen strings before? #string type variables s = “hello” #obtaining keyboard input from the user choice = input(“Please."

Similar presentations


Ads by Google