A service response is text. Order number, status code, amount, state — all of it arrives as pieces of text that have to be stored somewhere, combined and compared. That is what variables are for: a name with a value attached.
A variable is a name for a value
live example
status = 200
order_id = "ord-042"
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Free week →
Name on the left, value on the right, an equals sign between them. It is not the "equals" of mathematics but "put the right side into the left". After that the name gives you the value:
print(order_id) # prints ord-042
The name can be reassigned — that is what makes it a variable:
live example
status = 200
status = 404
print(status) # 404
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Free week →
Numbers and strings are different things
This is what everyone trips over first:
live example
code = "200" # a string: three characters of text
code = 200 # a number: you can do arithmetic with it
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Free week →
The quotes decide everything. For a person there is no difference, for a program there is a huge one:
live example
print("200" + "1") # 2001 — strings are glued together
print(200 + 1) # 201 — numbers are added
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Free week →
Mix them and the program stops with TypeError: can only concatenate str (not "int") to str. In plain words: a string can only be glued to a string. This is a
common situation: everything arrives from a service as text, and arithmetic needs
numbers.
String to number:
live example
total = int("4990") # 4990, now a number
price = float("99.90") # 99.9
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Free week →
And back:
live example
text = str(4990) # "4990"
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Free week →
int() on the string "four" fails — and rightly so: there is nothing to
convert.
Strings: the daily material
A string goes in quotes — single or double, it makes no difference.
Length:
live example
print(len("ord-042")) # 7
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Free week →
Substitution — the f-string, the convenient way. Put an f before the quotes
and write the expression in curly braces:
live example
order_id = "ord-042"
total = 4990
print(f"Order {order_id} for {total} RUB")
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Free week →
Searching inside a string — the in operator:
live example
response = "Order not found"
print("not found" in response) # True
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Free week →
The result is True or False. Those two values carry every check you will ever
write.
Useful string operations:
live example
" ord-042 ".strip() # "ord-042" — trim spaces
"ORD-042".lower() # "ord-042"
"ord-042".startswith("ord") # True
"ord-042".split("-") # ["ord", "042"]
"ord-042".replace("ord", "x") # "x-042"
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Free week →
strip() and lower() save you when comparing data from different sources: the
export says "Anna ", the database says "anna", and to the eye it is the same
thing — to the program it is not.
Common beginner mistakes
Unclosed quotes — print("hello). A SyntaxError with the line number;
check quote and bracket pairs.
Typo in a name — you defined order_id and used order_di. NameError: name 'order_di' is not defined, that is, "I do not know that name".
Compared a number with a string — 200 == "200" is False. Not an error but
a silent "did not match": the program does not crash, the test just behaves
strangely. Fix it by converting to one type.
What to solve
In short
- A variable is a name for a value;
=stores, it does not compare. "200"and200are different: text from a service must be converted withint()orfloat()before arithmetic.- An f-string (
f"Order {order_id}") is the normal way to build a message. strip()andlower()bring data from different sources to a comparable form.inanswersTrueorFalse— every check starts there.
What to read next
- Comparisons and conditions — how a program makes a decision.
- Lists and dicts — what JSON looks like from the inside.