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:
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!
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 are stored 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) print(s) hell owo rld!
String indexing practice s = “Mr. Paul Bui” print(s, s) #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 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
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