E ExamMaster

Engineering Python · Language Basics

Strings in Depth

In Engineering Python because lab text is measured and searched — length, substring checks and counts are the everyday string tools.

A string is a sequence of characters. Length is how many characters it holds; `in` and `.count` ask whether a piece appears and how often. Some lab papers forbid a for/if for vowel counting — you can still add the five `.count` results.

  • Engineering Python
  • Easy level
  • 4 concepts

1Length is a character count

The length of a string is the number of characters you can index, starting at 0. For `s = "lab"`, the valid indices are 0, 1 and 2 — three characters.

`len(s)` returns that count. Computing it by listing indices first is the teaching beat; `len` is the tool you then use.

Figure. Length is the number of characters you can index, starting at 0. For s = "lab", indices 0, 1, 2 — three characters. len(s) is 3.

Then use len

  1. SeeList the characters at 0, 1, 2 for a 3-letter word.
  2. Name`len(s)` is that count. Do not write a loop for it in later labs.

Index then len

s = "lab"
print(s[0], s[1], s[2])
print(len(s))

Count the letters in lab

s is "lab". List the indices, then name the length.

  • s[0], s[1], s[2]l, a, b
  • how many indices3
  • len(s)3

Pro tip. len is that count. A later lab that forbids a loop still wants len, not a for.

What is `len("lab")`?
  1. 3
  2. 2
  3. 4

Three characters: l, a, b. Indices 0, 1, 2.

2Substring check and count

`needle in haystack` is True when that exact piece appears. It is a membership test, not a loop you write.

`.count(piece)` returns how many non-overlapping times the piece appears. `.find(piece)` returns the first index, or -1 if missing.

Figure. msg is "lab-report". in tests membership. count returns non-overlapping hits. find returns the first index, or -1 if missing.

in, count, find

msg = "lab-report"
print("lab" in msg)
print(msg.count("p"))
print(msg.find("report"))

Find report inside lab-report

msg is "lab-report". Does "lab" appear? How many p? Where does "report" start?

  • "lab" in msgTrue
  • msg.count("p")1
  • msg.find("report")4

Pro tip. Indices start at 0: l a b - r … so report begins at 4.

3Vowel count without control flow

A vowel count is five membership counts added together. You do not need `for` or `if`: call `.count` once per vowel and add.

Lowercase first so `E` and `e` share a count. The compact form `sum(word.count(v) for v in "aeiou")` is the same five additions written as a generator — still no `if`.

Figure. A vowel count is five .count calls added together. In "engineering": 0+3+2+0+0 = 5. No for, no if.

Summed .count

word = "engineering"
n = (
    word.count("a")
    + word.count("e")
    + word.count("i")
    + word.count("o")
    + word.count("u")
)
print(n)

Vowels in engineering

Count a, e, i, o, u in "engineering" (already lowercase).

  • count("a") + count("e")0 + 3
  • 0 + 3 + count("i") + count("o") + count("u")0 + 3 + 2 + 0 + 0
  • 0 + 3 + 2 + 0 + 05

Pro tip. engineering has e,e,i,e,i — five vowels. A for-loop that checks each letter is the thing this lab paper forbids.

4Lab: length, substring, vowels

Print the length of a word, whether a piece appears, and a no-loop vowel count.

Figure. For word = "idea": print len, whether "de" is in the word, then the summed vowel count. Stdout contains 4 and 3.

Takeaway

  1. Hold thisPrint the length of a word, whether a piece appears, and a no-loop vowel count.

Coding lab. Measure a word runs in the app, with checks on your output.

Notes

  • In Engineering Python because lab text is measured and searched — length, substring checks and counts are the everyday string tools.
  • The length of a string is the number of characters you can index, starting at 0. For `s = "lab"`, the valid indices are 0, 1 and 2 — three characters.
  • `needle in haystack` is True when that exact piece appears. It is a membership test, not a loop you write.

Exam traps & shortcuts

  • Run one cell at a time and read stdout before changing more lines.
  • Names are labels for values; rebinding a name does not rewrite old prints.

Recap

Length is a character count; in/count search; vowels add five counts.

Length is a character count
The length of a string is the number of characters you can index, starting at 0. For `s = "lab"`, the valid indices are 0, 1 and 2 — three characters.
Substring check and count
`needle in haystack` is True when that exact piece appears. It is a membership test, not a loop you write.
Vowel count without control flow
A vowel count is five membership counts added together. You do not need `for` or `if`: call `.count` once per vowel and add.
Lab: length, substring, vowels
Print the length of a word, whether a piece appears, and a no-loop vowel count.

Practise Strings in Depth

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • A 2-question practice set that ends the chapter
  • 1 quick check with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.