Skip to main content

Foundations of Python Programming: Functions First

Section 9.9 Non-mutating Methods on Strings

There are a wide variety of methods for string objects. Try the following program.
In this example, upper is a method that can be invoked on any string object to create a new string in which all the characters are in uppercase. lower works in a similar fashion changing all characters in the string to lowercase. (The original string ss remains unchanged. A new string tt is created.)
You’ve already seen a few methods, such as count and index, that work with strings and are non-mutating. In addition to those and upper and lower, the following table provides a summary of some other useful string methods. There are a few activecode examples that follow so that you can try them out.
Table 9.9.1.
Method Parameters Description
upper none Returns a string in all uppercase
lower none Returns a string in all lowercase
count item Returns the number of occurrences of item
index item Returns the leftmost index where the substring item is found and causes a runtime error if item is not found
strip none Returns a string with the leading and trailing whitespace removed
replace old, new Replaces all occurrences of old substring with new
format substitutions Involved! See Subsection 9.9.1, below
You should experiment with these methods so that you understand what they do. Note once again that the methods that return strings do not change the original. You can also consult the Python documentation for strings
Check your understanding

Checkpoint 9.9.2.

    What is printed by the following statements?
    s = "python rocks"
    print(s.count("o") + s.count("p"))
  • 0
  • There are definitely o and p characters.
  • 2
  • There are 2 o characters but what about p?
  • 3
  • Yes, add the number of o characters and the number of p characters.

Checkpoint 9.9.3.

    What is printed by the following statements?
    s = "python rocks"
  • yyyyy
  • Yes, s[1] is y and the index of n is 5, so 5 y characters. It is important to realize that the index method has precedence over the repetition operator. Repetition is done last.
  • 55555
  • Close. 5 is not repeated, it is the number of times to repeat.
  • n
  • This expression uses the index of n
  • Error, you cannot combine all those things together.
  • This is fine, the repetition operator used the result of indexing and the index method.

Subsection 9.9.1 FStrings

Until now, we have created strings with variable content using the + operator to concatenate partial strings together. That works, but it’s very hard for people to read or debug a code line that includes variable names and strings and complex expressions. Consider the following:
Or perhaps more realistically:
In this section, you will learn to write that in a more readable way:
In grade school quizzes a common convention is to use fill-in-the blanks. For instance,
Hello _____!
and you can fill in the name of the person greeted, and combine given text with a chosen insertion. We use this as an analogy: Python has a similar construction, better called fill-in-the-braces. fstrings, make substitutions into places in a string enclosed in braces. Run this code:
There are several new ideas here!
An fstring has a special form, where the string must start with an f outside the quotations, and with braces embedded. The data embedded in the braces will be substituted into the string. There are many variations on the syntax between the braces.
In the code above, this new string is assigned to the identifier greeting, and then the string is printed.
The identifier greeting was introduced to break the operations into a clearer sequence of steps. However, since the value of greeting is only referenced once, it can be eliminated with the more concise version:
There can be multiple substitutions, with data of any type. Next we use floats. Try original price $2.50 with a 7% discount:
If you used the data suggested, this result is not satisfying. Prices should appear with exactly two places beyond the decimal point, but that is not the default way to display floats.
fstrings can give further information inside the braces showing how to specially format data. In particular floats can be shown with a specific number of decimal places. For two decimal places, put :.2f inside the braces for the monetary values:
The 2 in the format modifier can be replaced by another integer to round to that specified number of digits.
This kind of format string depends directly on the order of the parameters to the format method. There are other approaches that we will skip here, such as explicitly numbering substitutions.
A technical point: Since braces have special meaning in a format string, there must be a special rule if you want braces to actually be included in the final formatted string. The rule is to double the braces: {​{ and }​}. For example mathematical set notation uses braces. The initial and final doubled braces in the format string below generate literal braces in the formatted string:
a = 5
b = 9
setStr = 'The set is {​{​{a}, {b}​}​}.'
Unfortunately, at the time of this writing, the ActiveCode format implementation has a bug, printing doubled braces, but standard Python prints {5, 9}.

Checkpoint 9.9.4.

    What is printed by the following statements?
    x = 2
    y = 6
    print('sum of {x} and {y} is {x+y}; product: {x*y}.')
  • Nothing - it causes an error
  • It is legal format syntax: put the data in place of the braces.
  • sum of {x} and {y} is {x+y}; product: {x*y}.
  • The outpput will be the data stored in the variables, not the variables themselves.
  • sum of 2 and 6 is 8; product: 12.
  • Yes, correct substitutions!
  • sum of {2} and {6} is {8}; product: {12}.
  • The braces do not appear in the final fstring.

Checkpoint 9.9.5.

    What is printed by the following statements?
    v = 2.34567
    print('{v:.1f} {v:.2f} {v:.7f}')
  • 2.34567 2.34567 2.34567
  • The numbers before the f in the braces give the number of digits to display after the decimal point.
  • 2.3 2.34 2.34567
  • Close, but round to the number of digits and display the full number of digits specified.
  • 2.3 2.35 2.3456700
  • Yes, correct number of digits with rounding!
You have attempted of activities on this page.