# How to effectively teach strings and string literal constants?

I am teaching a general introductory CS course to 18-19 year olds, with Python3 as a support language, and I am not satisfied with how some students respond to strings and string literals. That is some of them struggle to understand when you should use quotes and when not.

For example, when trying to open a file, they will write open(/the/path/to/the/file) instead of open("/the/path/to/the/file") and fail to correct themselves. This is just an example ; the question is not about file/filename confusion.

Conversely, some students, when asked to write a function a parameter of which is a string, will write

def example("s") :
#etc


There seem to be an underlying poor understanding of what a variable (or identifier) is. However these students are generally coping with programs that do not manipulate strings.

A related question (maybe I should post this one separately) is about the pros and cons of introducing strings before arrays/lists or the converse.

# Some background

This takes place in France, in the CPGE (Classes préparatoires aux Grandes Écoles), a two-year curriculum that students may enroll in at the beginning of higher education (post-baccalauréat or post-A level or equivalent) but is physically located in high schools (lycées) and prepare students for national competitive exams (concours).

As a result, the global content of the course is mandated nationwide (including the use of Python3 as an example language) ; I can only choose the order and the manner in which I teach things. The course is mandatory for students but is not the main field of the curriculum (I have 1hr/week of blackboard course and 1hr/week lab session, compared to 10 to 12 hr/week of math and 8 to 10 hr/week of physics).

Computer science is not taught in France before university/higher education. There is only some algorithmics, taught by math teachers as well as they can, using pseudo-language and calculators languages, and not dealing with any data structure (only numbers) and no functions. So I start from zero.

Even in CPGE, the idea of teaching CS as a field in itself is quite new and not universally accepted. As a result, the nationwide CS curriculum for CPGE mandates that problems and examples should be taken from math and physics situations, which leads to string being scarcely used except for file names and data input from files (csv). Yet any concours may ask a problem about strings, since they are mentioned in the nationwide curriculum.

Perhaps the place to start is with a discussion of variables and making the distinction between a "thing" and a "name for the thing."

A pretty good place to start that conversation is with the a poem from Through The Looking Glass, the sequel to Lewis Carroll's Alice in Wonderland.

1. The song's name is called Haddocks' Eyes
2. The song's name is The Aged Aged Man
3. The song is called Ways and Means
4. The song is A-sitting on a Gate

See wikipedia for a discussion and background as well as the poem itself.

In programming we don't normally use the first of these four distinctions, but the others are important. The second is how we think of a variable - a name for a thing. The fourth is the thing itself. The third can be an alias or alternate name for the thing, though that analogy isn't perfect.

But once the distinction between the name and the thing is made, you have a better chance of winning. But you still need to be clear.

"foo" is a thing (a String thing)
s = "foo" establishes s as a name for that thing.

"s" is a different (String) thing.

But "/the/path/to/the/file" is simultaneously a thing (String) but also a complicated name for another thing (a File). The open method needs a name for the file to be opened.

Note that much of the above was copied from an essay I wrote in meta.

• Thanks. I think the idea around the poem rather confusing; maybe it is because I do not know it (I doubt my students would) and cannot find of a well known equivalent in French. Your other suggestions correspond to what I am teaching presently (see question 1.3 there). I too remarked that students tend to confuse a file with its name; I will try to see if "x"/x confusions arise more in this context. Could you point me to your essay? I could not find it by browsing your profile. – ysalmon Aug 25 '17 at 18:53
• In English we can relate quoted string literals to what they learn in literacy class ( see call this English Language class). In French I thing you quote «Like this». I also note that in you text you have used ” in place of ". This may (or may not) lead to confusion. – ctrl-alt-delor Aug 25 '17 at 19:36
• @ysalmon. See cseducators.meta.stackexchange.com/q/276/1293 – Buffy Aug 25 '17 at 19:41
• At step 4. The white knight should simply have started singing after he said, "The song is". – Solomon Slow Aug 28 '17 at 21:34

After now two administrations of the course since I asked this question, I think I have found a way to increase my student's understanding of the problem.

First, I introduce strings earlier and more explicitly, and I talk about files and file names later.

Second, I explicitly teach about literal constants as expressions that "directly mean their value", without needing any calculation. This enables me to tell them that string literal constants (and not just strings) must be quoted. This is in agreement with my insistence to distinguish expressions and their values (eg. 3+2 is not an integer), which is something they have to get their heads around after 12hr/week of math where they absolutely do not make this distinction.

Third, there is a small exercise that I find excellent for beginners : have them write a Python function that, given a string of odd length, returns the middle character. This is conceptually trivial but has all the pieces that one needs and can have misconceptions about regarding strings and string indices :

• they must not use quotes in the name of the formal parameter, but must when testing their function with an example,
• they have to remember the notion of length, index, the indices range, and the associated syntax,
• bonus : they get to experience that even if len(s) is odd, (len(s)-1)/2 is not an integer, as Python is unusually picky about the type of the index.

More generally, I managed to shift some classical examples that I was doing with tuples to strings : eg. writing a function that returns whether a given string is an alternation of as and bs instead of whether a given integer tuple is an alternation of even and odd numbers.

• One of my favorite examples to show my intro students regarding string literals vs expressions is something like "12/12/10" could be representative of the date Dec. 12, 2010, vs 12/12/10 is going to be 0.1. – cryptic_star Oct 12 '18 at 10:02

The problem appears to be that algebraic substitution comes naturally to your students, since they have been introduced to it much earlier in their education - and I think the idea of a 'number in a box' is much simpler than the idea of a string in a computing context.

Strings are far more complex concepts than a number - there is the character set mapping, a list of unicode (since they expect accented characters) and a delimiter. For me, the absence of any detail of the implementation was a challenge when it came to learning about string variables - so I can easily imagine that string variables are the ones which stand out as being confusing.

It seems that you need to refresh the idea of variables being just a label for something, and also that the thing which is labelled is maybe just a representation, rather than an accurate copy.

I can't give any good culturally relevant examples, but maybe one way of relating to the quotation marks is to talk about picture frames. Obviously the variable names can be written on the back of the pictures... With a number it's usually quite clear where the boundary of a picture is. If you had a photo of a window and stuck it on the wall, it might seem to be an actual window - but the picture frame would be a clear recognisable sign that what is inside is a 'representation'. Pictures with addresses, street maps, phone numbers could also be used to play with some ideas about indirection and references.

I'd re-iterate that it seems to me that numeric variables are probably working for your students by chance, and you can maybe explain the basics what they have inferred in this aspect, and expand on it to include the other data types you need to cover.

I often see people mixing up files and filename in variable naming. It will result in mixed up thinking. I think it is important to come up with clear names.

I like to start with giving them lots of code. They then read the code, analyse, modify. In my code I use clear names. We discuss naming, and how this affects readability.

Also to discuss with class:

I have included some questions about some statement. Use your discretion as when to ask them. I am not suggesting you ask them in a sequence as presented.

When you call a subroutine it takes a thing or a name of a thing. e.g. open takes a string [or the name of a string]. Is `/dev/null' a string? Why is it not a string? Is it the name of a string? Why is it not the name of a string?

When you define a subroutine it takes a name of a thing. Why does it not take a thing, why only a name of a thing? Is that a name of a thing?