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

<p>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.</p>

<ol>
<li>The song's <em>name is called</em> Haddocks' Eyes</li>
<li>The song's <em>name is</em> The Aged Aged Man</li>
<li>The song <em>is called</em> Ways and Means</li>
<li>The song <em>is</em> A-sitting on a Gate</li>
</ol>

<p>See <a href="https://en.wikipedia.org/wiki/Haddocks&#39;_Eyes" rel="nofollow noreferrer">wikipedia</a> for a discussion and background as well as the poem itself.</p>

<p>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. </p>

<p>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. </p>

<p>"foo" is a thing (a String thing)<br>
s = "foo" establishes s as a name for that thing. </p>

<p>"s" is a different (String) thing. </p>

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

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

CC BY-SA 3.0
2017-08-25T18:53:57.210Z
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](https://pro.yannsalmon.fr/_media/forums/td-chaines.pdf)). 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
CC BY-SA 3.0
2017-08-25T19:36:28.560Z
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
CC BY-SA 3.0
2017-08-25T19:41:29.343Z
@ysalmon. See https://cseducators.meta.stackexchange.com/q/276/1293
Buffy
CC BY-SA 3.0
2017-08-28T21:34:05.180Z
At step 4. The white knight should simply have started singing after he said, "The song _is_".
Solomon Slow
CC BY-SA 3.0
2017-08-25T17:16:42.957Z
2017-08-25T17:16:42.957Z
Buffy
<p>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.</p>

<p>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.</p>

<p>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. </p>

<p>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.</p>

<p>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.</p>

CC BY-SA 3.0
2017-08-27T21:50:57.240Z
2017-08-27T21:50:57.240Z
Sean Houlihane
<p>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. </p>

<p>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.</p>

<p>Also to discuss with class:</p>

<p><em>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.</em></p>

<p>When you call a subroutine it takes a thing or a name of a thing. e.g. <code>open</code> 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?</p>

<p>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?</p>

CC BY-SA 3.0
2017-08-25T19:26:20.540Z
2017-08-25T19:26:20.540Z
ctrl-alt-delor
<p>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.</p>

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

<p>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. <code>3+2</code> 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.</p>

<p>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 :</p>

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

<p>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 <em>a</em>s and <em>b</em>s instead of whether a given integer tuple is an alternation of even and odd numbers.</p>

CC BY-SA 4.0
2018-10-12T10:02:19.200Z
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
CC BY-SA 4.0
2018-10-11T17:52:19.553Z
2018-10-11T17:59:02.083Z
2018-10-11T17:59:02.083Z
ysalmon
<p>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.</p>

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

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

<pre><code>def example("s") :
   #etc
</code></pre>

<p>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.</p>

<p>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.</p>

<h1>Some background</h1>

<p>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).</p>

<p>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).</p>

<p>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.</p>

<p>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.</p>

CC BY-SA 3.0
2017-08-25T15:24:26.487Z
Hi ysalmon! Welcome to [cseducators.se]! This is a very well organized question. We hope you'll stick around and maybe say "Hi" [in our chat room](https://chat.stackexchange.com/rooms/59174/the-classroom).
thesecretmaster
<curriculum-design><lesson-ideas><introductory-lesson>
How to effectively teach strings and string literal constants?
# How to effectively teach strings and string literal constants?

(Asked by: username_0 on 2017-08-25)

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.

**Tags:** curriculum-design, lesson-ideas, introductory-lesson

> **Comments:**
> **username_1**: Hi username_0! Welcome to [cseducators.se]! This is a very well organized question. We hope you'll stick around and maybe say "Hi" [in our chat room](https://chat.stackexchange.com/rooms/59174/the-classroom).  

---

## 4 answers

### Answer by username_0 on 2018-10-11. Score: 2 | Accepted answer

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 *a*s and *b*s instead of whether a given integer tuple is an alternation of even and odd numbers.

> **Comments:**
> **username_6**: 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.  

---

### Answer by username_2 on 2017-08-25. Score: 3

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](https://en.wikipedia.org/wiki/Haddocks'_Eyes) 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.

> **Comments:**
> **username_0**: 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](https://pro.yannsalmon.fr/_media/forums/td-chaines.pdf)). 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.  
> **username_3**: 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.  
> **username_2**: @username_0. See https://cseducators.meta.stackexchange.com/q/276/1293  
> **username_4**: At step 4. The white knight should simply have started singing after he said, "The song _is_".  

---

### Answer by username_5 on 2017-08-27. Score: 1

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.


---

### Answer by username_3 on 2017-08-25. Score: 0

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?


---

