thinkcspy icon indicating copy to clipboard operation
thinkcspy copied to clipboard

Chapter 9 String Interpolation

Open pearcej opened this issue 3 years ago • 6 comments

The PEP standards include two methods for string interpolation. See https://www.python.org/dev/peps/pep-3101/

Python currently provides two methods of string interpolation:

The '%' operator for strings.

  • The string.Template module which uses .format like "My name is {0}".format('Fred')
  • The '%' operator for strings like: "My name is %s" % ('Fred')

But perhaps we should be presenting the simpler syntax of f-strings as described in https://www.python.org/dev/peps/pep-0498/ as recommended by @bnmnetp below?

pearcej avatar Oct 06 '21 15:10 pearcej

How many should we show. In my opinion we should be moving toward the future. In fact my preferred method is to teach students f-strings

name = ‘Fred’
print(f”my name is {name}”)

The % operator is still good/interesting to show students as it maps to C/C++/Java but it is definitely not mainstream Python in 2021.

bnmnetp avatar Oct 07 '21 00:10 bnmnetp

Very fair point. I have updated my Issue.

pearcej avatar Oct 07 '21 10:10 pearcej

I am working on this issue

ConceptaNjolima avatar Nov 28 '21 20:11 ConceptaNjolima

@bnmnetp @pearcej Is the suggestion that we change everywhere the format method is used to using an f-string or only change Activity: 9.5.1.2 ActiveCode?

ConceptaNjolima avatar Nov 28 '21 20:11 ConceptaNjolima

I think students should be exposed to all of the various string interpolation types so that they understand what they see when they run across code. But I think we should isolate .format and the old printf style formats to a section and then use fstrings consistently throughout.

bnmnetp avatar Nov 28 '21 21:11 bnmnetp

I have been convinced that @bnmnetp is right about this.

pearcej avatar Dec 04 '21 12:12 pearcej