220 likes | 583 Views
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
E N D
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 • http://www.wikipedia.com • http://www.paulbui.net/wl/IB_Computer_Science_1
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?
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 = “”
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
String indexing practice s = “Mr. Paul Bui” print(s[4], s[9]) #Try it out with your name #Print out your initials using indexing
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
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
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
String Concatenation • Concatenation means connecting • Combine strings using + (concatenation operator) firstName = “John” lastName = “Doe” fullName = firstName + “ ” + lastName
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
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”
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?
String concatenation in a loop build = “”x = 0 while x < 5: build = build + “a” x += 1 print(build)
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