Python Part I · Module 2 · Variables and Data Types
Text: Your First Strings
Text has appeared in nearly every example so far without being properly introduced. This chapter introduces it as a value type: what Python calls it, how to create one, and the single distinction that matters more than anything else in the chapter — that a piece of text containing digits is not a number. That sounds obvious stated plainly and is the source of a great many real bugs, because everything a script reads from a log, a report or a command line arrives as text regardless of what it looks like. This is a deliberately focused chapter. Text is important enough to have a whole module of its own later; here we establish only what it is, so the next two chapters can teach you how to find out what you are holding and how to convert it.
Foundation8 min readPythonPart IFundamentalsStringsstr
Part I · Module 2 · Chapter 2.9 · Text: Your First Strings
1. Text Is a Value
test_name = "axi_smoke"
run_dir = "runs/axi_smoke_4210"
print(test_name)
print(run_dir)axi_smoke
runs/axi_smoke_4210A piece of text is a value, exactly as a number is. Python calls this type str, short for string — the traditional programming word for a sequence of characters.
The quotes work as Chapter 1.8 §1 described: they mark where the text starts and stops, and they are not part of the text itself. Single and double quotes are equivalent, as long as they match.
a = "axi_smoke"
b = 'axi_smoke'
print(a)
print(b)axi_smoke
axi_smokeIdentical. This course uses double quotes throughout for consistency.
2. Text Can Contain Anything
The characters inside the quotes are not interpreted. Python does not try to understand them, evaluate them, or check them against anything.
print("4210")
print("10 / 4")
print("True")
print("# this is not a comment")4210
10 / 4
True
# this is not a commentThat last one is worth noticing. A # inside quotes is just a character. Comments, from Chapter 1.9, only work outside text.
3. The Distinction That Matters
Here it is, stated as directly as possible.
seed = 4210
seed_text = "4210"
print(seed)
print(seed_text)4210
4210Identical output. Completely different values.
4210 is a number. Python can do arithmetic with it, compare it against other numbers, use it as a count.
"4210" is four characters — 4, 2, 1, 0 — sitting next to each other. Python cannot do arithmetic with it, because it is not a number. It happens to look like one to a human reader, and that is the whole problem.
4. Completing the Earlier Distinctions
Chapter 2.8 flagged two of these and could not finish them until text existed. Now it can.
real_bool = True
text_bool = "True"
real_none = None
text_none = "None"
print(real_bool)
print(text_bool)
print(real_none)
print(text_none)True
True
None
NoneFour lines of output, and only two distinct kinds of value among them. "True" and "None" are strings — text that spells a word. True and None are the values themselves.
The pattern is now consistent across the whole module: quotes make text, and text is never the thing it spells.
5. Text Is Not Only for Words
Worth stating, because engineering data is full of text values that are not prose:
test_name = "axi_smoke"
run_dir = "runs/axi_smoke_4210"
status = "PASS"
log_path = "sim.log"
severity = "UVM_ERROR"A path is text. A status word is text. A file name is text. None of these needs any explanation beyond it is a sequence of characters, which is what str is for.
6. Predict, Run, Observe
Predict the output of each, then run the file.
print("500")
print(500)
print("0.8")
print("False")
print("")500
500
0.8
FalseThe last one is an empty string — a piece of text with no characters in it. It printed an empty line. It is a perfectly valid value, and note that it is not None: empty text is a message with nothing in it, while None is the absence of a message. Chapter 2.8 §6 made that distinction and this is what it looks like.
7. What Is Coming Later
Text is a large topic and this chapter deliberately covers only the type. Module 4 — Strings teaches what you can actually do with one:
- reaching individual characters, and pieces of a longer piece of text;
- measuring its length and testing whether something appears inside it;
- splitting a line into fields and joining pieces back together;
- changing case, trimming stray spaces, replacing parts;
- building text with values embedded in it.
That module is where a log line becomes something a script can take apart. It is nine modules of prerequisites away because it uses all of them.
For Module 2 you need only this: text is a value, its type is str, and it is not a number even when it looks like one.
8. Exercise
Exercise 1 — five facts. Write a file giving names to five pieces of text from a run: a test name, a status word, a log file name, a run directory, and a severity level. Print each.
Exercise 2 — number or text. For each, say whether it is a number or text, without running anything:
(a) 4210 (b) "4210" (c) 0.8 (d) "0.8" (e) "PASS" (f) True (g) "True"
Exercise 3 — the empty pair. Write two names, one referring to "" and one to None. Print both. Describe in one sentence what a reader learns from the output, and what they cannot learn from it.
Exercise 4 — reason about a log. A line read from a simulation log says seed = 4210. After your script reads that line, is the 4210 in it a number or text? Explain in one sentence what that means for a script that wants to add one to it.
9. Summary
- Text is a value type, called
str, created with matching single or double quotes. - Quotes mark the boundaries and are not part of the text, as Chapter 1.8 established.
- The characters inside are never interpreted — a
#inside quotes is a character, not a comment. "4210"is not4210. One is four characters; the other is a number. They print identically."True"is notTrueand"None"is notNone, for the same reason.- Everything read from a file, report or command line arrives as text, including the numbers. This is why Chapter 2.11 exists.
- Empty text
""is notNone— one is a message with no characters, the other is no message. - Module 4 teaches what you can do with text. This chapter established only what it is.
You have now met every value type in Module 2. The next chapter gives you a way to stop guessing which one you are holding.
Where this fits
Part of the Python curriculum.
