Module 5: Strings and Characters | CMSC 105 Elementary Programming - Fall 2024

Module 5: Strings and Characters

Note: Create a text file called module5.txt where you will store you answers to exercise questions. The questions that are not related to changing code. You will submit this file on Blackboard along with your code.

Objectives

By the end of this module you will be able to:

Strings

About strings:

Just like integer values can be placed in variables, we can do the same with strings:

Consider this example:

# Make a string and print it:
s = 'The quick brown fox jumps over the lazy dog'
print(s)

# Extract the length of the string and print that:
k = len(s)
print(k)
# The string s goes into the function len.
# The resulting integer is placed in the variable k.
k = len(s)  

# The value in k, after the length has been computed
# is then printed
print(k)

Exercise 1: Type up the above in my_string_example.py. What is the printed length? Remember, answers to non-coding questions like this need to be in the appropriate module text file, in this case module5.txt.


String Concatenation

Strings would be of limited use if there were no way of combining them, just as integers would be if there were no way of performing arithmetic.

The joining of two strings end to end is called concatenation.

Consider this program:

x = 'The'
y = 'quick'
z = 'brown'

s = x + y + z
print(s)

Exercise 2: Type up the above in my_string_example2.py. What is the output?


About concatenation:

Exercise 3: Type up the above in my_string_example3.py. What is the output? How many times did a concatenation occur?


We also introduced something new:

print(len(s))

Here’s how to read this line:

Often we want to concatenate strings with numbers, or other kinds of things:

For example: consider

k = 26
s = str(k)
t = 'A pangram must have all ' + s + ' letters'
print(t)
s = str(k)

Exercise 4: Type up the above in my_string_example4.py to confirm.


Let’s examine a small variation:

k = 26
# You can also build a string in the print statement itself:
print('A pangram must have all ' + str(k) + ' letters')

Exercise 5: Type up the above in my_string_example5.py to see the resulting output.


Variable names

Most often, we’ve been using single letter variable names, for example:

i = 7
j = 15 * i
print(j)

x = 'Hello' 
y = 'World'
z = x + ' ' + y
print(z)

Let’s rewrite the above with more meaningful variable names:

days_in_a_week = 7
days_in_a_semester = 15 * days_in_a_week
print(days_in_a_semester)

first_greeting_word = 'Hello' 
second_greeting_word= 'World'
full_greeting = first_greeting_word + ' ' + second_greeting_word
print(full_greeting)

About variable names:

Exercise 6: Let’s see what can go wrong if we use one of the words “already taken”. Consider this program:

s = 'Hello'
len = 5       # This is a BAD idea
print(len)

We really should not be using a variable called len since len is a predefined “already taken” name. What happens if you type and run this? Type your program in my_variable_name.py.

Then, type this program in my_variable_name2.py:

s = 'Hello'
len = 5       # This is a BAD idea
print(len)

t = 'Some looooooong sentence'
k = len(t)
print(k)

What do you see? (Respond in your module5.txt.)


When things go wrong

In each of the exercises below, try to identify the error before typing it up and confirming. Report the error in the module.txt. Then, fix the error in the code, using the specified program (.py) name.

Exercise 7:

x = 'Hello'
y = 'World'
s = 'x' + ' ' + 'y'

This should print Hello World, with a space in between. Fix the error in error1.py.


Exercise 8:

k = 8
s = 'There are ' + k + ' planets in our solar system'
print(s)

Fix the error in error2.py.


Exercise 9:

long_sentence = 'How' + ' ' + 'vexingly' + ' ' +
   'quick' + ' ' + 'daft' + ' ' + 'zebras' + ' ' + 'jump'

Fix the error in error3.py.


Exercise 10:

x = input('Enter a number between 1 and 10')
y = 2 * x
print(y)

Fix the error in error4.py.