1 of 9

CS149 – Strings

2 of 9

Model 1 Character Arrays

In Python the type String encapsulates an array of characters so a single character is a String of length 1.

letter = ‘a’

array = “cat”

word = “dog”

“A”

“dog”

“cat”

letter→

array→

word→

3 of 9

Indexes

  • What is the index of 'd' in the string above? What is the index of 'g'? In general, what is the index of the last character of a string?

A

“dog”

“cat”

letter→

array→

word→

4 of 9

Memory Stack Diagrams

  • Draw a memory diagram for the given code. Each variable should be a name next to a box containing its value.

str = “Hi!”��let = ‘X’

num = -1

hmm = str

5 of 9

Modifying strings

b = "Hello, There!"

print(b[6:10]) # prints "The"

print(b[:5]) # prints "Hello"

print(b[7:]) # prints "There!"

a = " Hello, World! "

print(a.strip()) # prints "Hello, World!"�a = "Hello, World!"

print(a.replace("H", "J")) # prints ['Jello', ' World!']

a = "Hello, World!"

print(a.split(",")) # prints ['Hello', ' World!']

6 of 9

String Methods

If txt contains the string "hello world", then what is the return value of the following

String method calls?

a) txt.index(“ll”)

b) txt.endswith("ld")

c) len(txt)

d) txt[4: 7]

e) txt.upper()

capitalize() - Converts the first character to upper case

count() - Returns the number of times a specified value occurs in a string

endswith() - Returns true if the string ends with the specified value

find() - Searches the string for a specified value and returns the position of where it was found

index() - Searches the string for a specified value and returns the position of where it was found

isdecimal() - Returns True if all characters in the string are decimals

lower() - Converts a string into lower case

replace() - Returns a string where a specified value is replaced with a specified value

split() - Splits the string at the specified separator, and returns a list�upper() - Converts a string to upper case

7 of 9

Common Mistakes

1. Write the output of each program in the space under the table above. What is the logic

error you see when you run Program A?

2. In Program A, what is returned by the string method? What happens to the return value?

3. Describe two different ways you can fix the logic error in #17.

4. What is the output of program B if you enter letter as the input

Program A

Program B

greeting = “hello world”

greeting.upper()

print(greeting)

line = input(“Enter a word:”)

letter = line[1]

print(letter)

8 of 9

Issues

Trying to get the check the last element of a blank string can create an issue if you use -1 indexing

For example:

text = ""

last = text[-1]

print(last)

This will produce a runtime error (we saw this in the Eight ball lab). If you are looking at this

you might be able to use the endswith() to check and ending character.

txt = "Hello, welcome to this class."

last = txt.endswith(".")

print(x)

Or you can do an if len(text) > 0 before pulling that last character.

9 of 9

  • Acknowledgements

Parts of this activity are based on materials developed by Chris Mayfield and Nathan Sprague.

</end>