Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.

Similar presentations


Presentation on theme: "Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence."— Presentation transcript:

1 Unit 4 – Chapter 4 Strings and Tuples

2 len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence. A sequence length is the number of elements it has. Ex. string = “Hello World!” len(string) --------  12 What would be the length of the string “Hi my name is Jim.”?

3 In Operator You can use the in operator anywhere in your programs to check if an element is a member of a sequence. _______ in _______ element sequence Ex. If “e” in message: print (“is in your message”) else: print(“is not in your message”) If it is a member it is True, else it is False

4 Indexing Strings When you use a for loop you are using sequential access. This is like wanting to get to the box at the bottom of a pile and having to lift all of the heavy boxes on the top first before you make it to the bottom. When you index strings you are using random access. This is like just being able to grab the box you want without having to lift any other boxes. *Random Access Program

5 Indexing Strings Continued Note: Positions start at 0, so the last element in a sequence is at the position # of its length minus 1. Ex. In the string “heart” the length is 5 so the letter t would be in position 4. Working with Negative Position Numbers -With negative position numbers you count backwards starting with the last letter. Ex. String = “pizza pie” string [-5] = 1 string [-4] = “ “ string [-1] = e

6 String Immutability Once you create a string, you can’t change it. (Imagine writing on paper in ink) If you were to say word = “game” word [0] = “l” You will get an error. You can’t change the g in game to an l. Note: This is not the same as name = “Ms”. A then name = “Jim”. Here you are just assigning the same variable a different name.

7 Building a New String Even though you can’t change a string, you can build a new string one element at a time. No Vowels Program: # No Vowels # Demonstrates creating new strings with a for loop message = input("Enter a message: ") new_message = "" VOWELS = "aeiou” print() for letter in message: if letter.lower() not in VOWELS: new_message += letter print("A new string has been created:", new_message) print("\nYour message without vowels is:", new_message) input("\n\nPress the enter key to exit.") VOWELS is a constant that is intentionally capitalized. This is an indication that this string should not change Throughout the program. letter.lower() will change each letter in message to lowercase since you don’t care about the case when you are checking to see whether the letter is a vowel. Concatenates a new string adding each letter that is not a vowel as the loop runs.

8 Slicing Strings With indexing, you don’t have to copy just one element at a time, you can make copies of continuous sections of elements (called slices). With slicing you can grab anything from a one element to a group of elements to the entire string. Ex. Pizza Slicer Programp i z z a word = “pizza” word [0:5]  pizza word [1:3]  iz word [-4:-2]  iz word [-4:3]  iz

9 Be Careful! Note: If you create an “impossible” slice, where the starting point is bigger than the ending point, like word [2:1], you won’t cause an error. Instead, Python will quietly return an empty sequence. For strings, that means you’ll get the empty string. So be careful, because this is probably not the kind of result you’re after.

10 Slicing Shorthand You can omit the beginning point for the slice to start the slice at the beginning of the sequence. p i z z a Ex. word[:4] is the same as word[0:4] You can omit the ending point for the slice so that it ends with the last element. Ex. word[2:] is just shorthand for word [2:5] You can even omit both numbers to get a slice that is the entire sequence. Ex. word[:] is shorthand for word[0:5]


Download ppt "Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence."

Similar presentations


Ads by Google