Skip to content
VLSI Mentor

Python Part I · Module 4 · Strings

Working With Strings Before Reaching for Regex

Module 2 established what a string is: a value that holds text. This module is about what you can do with one. It opens with a tour, because the most useful thing to know at the start is how much ordinary Python already handles. A line of output from a tool is just text, and asking whether it contains a marker, how long it is, what its fields are, or how it begins takes no pattern language and no libraries. That matters beyond convenience: engineers who never learn the plain tools reach for regular expressions reflexively, and regex is harder to read, harder to review and easier to get subtly wrong. This chapter also introduces the one piece of new notation the rest of the module leans on — the dot.

Foundation10 min readPythonPart IFundamentalsStringsText

Part I · Module 4 · Chapter 4.1 · Working With Strings Before Reaching for Regex

1. A Line of Output Is Just Text

When a tool finishes, it leaves behind text. A simulator prints lines to a log. A build writes a report. A script you run tomorrow will need to answer questions about that text: did anything fail, how long did it take, which test was it.

Here is one such line. You do not need to know which tool printed it — for our purposes it is simply a piece of text, exactly like the strings in Chapter 2.9:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
line = "UVM_ERROR @ 1200 ns: scoreboard mismatch"
print(line)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
UVM_ERROR @ 1200 ns: scoreboard mismatch

Nothing new so far. line is a name; the value it refers to is a string of 40 characters. What is new is the question: what can a script ask about it?

Start with the one you can already write. Chapter 3.6 taught membership, and membership works on text:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
line = "UVM_ERROR @ 1200 ns: scoreboard mismatch"
print("UVM_ERROR" in line)
print("UVM_WARNING" in line)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
True
False

That expression — "UVM_ERROR" in line — is the single most common text operation in real scripts. It is an expression like any other from Module 3: it evaluates to a bool. You have been able to write it since Module 3, and it already answers a genuinely useful question.

This module is the rest of the answer.

2. The Tour

How long is the text? (Chapter 4.6)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
line = "UVM_ERROR @ 1200 ns: scoreboard mismatch"
print(len(line))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
40

How does it begin? How does a path end? (Chapter 4.10)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
line = "UVM_ERROR @ 1200 ns: scoreboard mismatch"
path = "runs/axi_smoke/sim.log"
print(line.startswith("UVM_ERROR"))
print(path.endswith(".log"))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
True
True

Get rid of stray spaces. (Chapter 4.7)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
raw = "  PASS  "
print(raw.strip())
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
PASS

Break one line into its separate fields. (Chapter 4.8)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
record = "axi_smoke 4210 PASS 182.4"
print(record.split())
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
['axi_smoke', '4210', 'PASS', '182.4']

The square brackets in that output are Python showing you several values grouped together. Chapter 4.8 says the necessary minimum about that, and Module 5 is devoted to it. For now, notice only that one string went in and four separate strings came out.

Substitute one piece of text for another. (Chapter 4.10)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print("a\tb".replace("\t", " "))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
a b

Build new text with values inside it. (Chapter 4.12)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
test_name = "axi_smoke"
seed = 4210
print(f"runs/{test_name}_{seed}/sim.log")
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
runs/axi_smoke_4210/sim.log

Six operations, no imports, no pattern language. Between them they cover asking about text, cleaning text, taking text apart and putting text together — which is most of what scripts do with text.

3. The Dot: How to Read value.method(...)

Look again at one of those lines:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
raw.strip()

This is new notation and it is worth a minute, because it appears in almost every remaining chapter of this module.

Read it in three pieces:

  • raw — the value you are working with. A string.
  • .strip — the operation you want performed on that value. An operation attached to a value like this is called a method.
  • () — do it. The parentheses are how you actually call the operation, and any extra information it needs goes inside them.

Said in one sentence: raw.strip() means "take the string raw and give me back the stripped version of it."

Two consequences follow, and both matter more than the notation.

First, a method call is an expression. Everything Module 3 taught about expressions applies. It evaluates to a value, so you can print it, give it a name, or use it inside a bigger expression:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
raw = "  PASS  "
clean = raw.strip()
print(clean)
print(clean == "PASS")
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
PASS
True

Second, and this is the one beginners get wrong: the method gives you a new string back. It does not edit raw.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
raw = "  PASS  "
raw.strip()
print(raw)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
  PASS

The strip() on line 2 did its work perfectly — and the result was thrown away, because nothing was done with it. raw still refers to the original text, spaces and all. That is not a quirk of strip; it follows from the immutability Chapter 2.13 established, and Chapter 4.11 is devoted to it.

Some methods need extra information, and it goes in the parentheses:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print("a-b".replace("-", "_"))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
a_b

Here replace needs two pieces of information — what to look for and what to put in its place — so it takes two, separated by a comma. You have seen that shape before: print() takes what to print between its parentheses in exactly the same way.

4. What This Module Covers

The tour was deliberately unordered. The module is not. It moves from making text, to reaching into it, to asking about it, to transforming it, to building new text from values:

ChaptersWhat you will be able to do
4.2 – 4.3Write any text you need, including quotes and multiple lines; combine and repeat strings
4.4 – 4.5Reach a single character, and take out a piece of a longer string
4.6Measure text, and test whether something appears inside it
4.7Normalise case and remove stray whitespace
4.8 – 4.9Break one line into fields, and build one line from separate pieces
4.10Test how text begins and ends, locate a marker, substitute text
4.11Explain precisely why a string method returns rather than modifies
4.12 – 4.13Build text with values embedded in it, and format numbers so a report lines up

By the end you will be able to read and write ordinary Python string code without another beginner tutorial.

5. The Judgement This Module Is Really Teaching

There is a tool called a regular expression — regex for short. It is a small language for describing patterns in text, and Part II teaches it properly. It is powerful and sometimes exactly right.

It is also the most over-reached-for tool in scripting. The reason is habit, not need: an engineer who has only ever solved text problems with regex will reach for it even when in and split would have done the job in one readable line.

Compare honestly. To answer "does this line report an error", the plain version is:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
line = "UVM_ERROR @ 1200 ns: scoreboard mismatch"
print("UVM_ERROR" in line)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
True

A regex version of the same question needs an import, a pattern written in a second language, and a reader who knows that language. It answers no additional question.

That is a judgement, and a judgement needs both options on the table. Most courses teach you only the second one. This module gives you the first, so that when Part II hands you regex you will know when not to use it.

6. Predict, Run, Observe

Predict each result before reading on. The first two use only Module 3 knowledge; the third is the point of the exercise.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
line = "PASS: axi_smoke"
print("PASS" in line)
print("pass" in line)

text = "  spaced  "
text.strip()
print(text == "  spaced  ")

Now the verified output:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
True
False
True

The first is True — "PASS" does appear. The second is False, and that surprises people: membership on text is case-sensitive, because "P" and "p" are different characters. Chapter 4.7 is largely about dealing with that.

The third is the important one. text.strip() ran, produced "spaced", and that result went nowhere. So text still refers to the original ten-character string, and comparing it against the original gives True. If you predicted False, you predicted that a method modifies its value — the assumption this chapter exists to remove.

7. Exercises

Exercise 1 — read the notation. For each, name the value being worked on, the operation, and the extra information given to it. Do not run anything: (a) path.endswith(".log") (b) line.strip() (c) record.split(",") (d) name.replace("-", "_")

Exercise 2 — predict membership. Given line = "UVM_WARNING: setup violation", say True or False for each, then check: (a) "UVM" in line (b) "ERROR" in line (c) "warning" in line (d) "" in line

The last one is worth thinking about before you run it.

Exercise 3 — the thrown-away result. This script prints the wrong thing. Explain in one sentence what the author expected, what actually happened, and change one line to fix it:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
status = "  FAIL  "
status.strip()
print(f"[{status}]")

Exercise 4 — plain or pattern. For each task, say whether ordinary string operations are enough or whether a pattern language is genuinely needed. One sentence each: (a) does this line contain the word ERROR (b) does this filename end in .log (c) extract the number from seed = 4210 where the spacing around = may vary (d) split a,b,c into its three parts

Exercise 5 — argue the other side. Write two sentences that a reviewer could use to justify rejecting a regular expression in a code review, without claiming regex is bad.

8. Summary

  • A line of tool output is an ordinary string. Everything Module 2 said about string values applies to it.
  • "MARKER" in line is the most common text test there is, it is an expression producing a bool, and you could already write it after Chapter 3.6.
  • Membership on text is case-sensitive. "pass" in "PASS: x" is False.
  • value.method(...) reads as "take this value and perform this operation on it." The value is on the left, the operation after the dot, and parentheses perform it.
  • A method call is an expression — it evaluates to a value, so it can be printed, named or used inside a larger expression.
  • A string method returns a new string and never modifies the original. A method call on a line by itself throws its own result away.
  • Plain string operations cover most real text work: asking, cleaning, splitting, joining, building.
  • Regex is a judgement, not a default. Learn the plain tools first so the judgement is available to you.

The next chapter starts at the beginning: writing down exactly the text you want, including text that contains quotes of its own.

Where this fits

Part of the Python curriculum.