Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

Strings

A string is Python's type for text, and it comes with methods for cleaning, searching and building text such as prompts.

Last updated: 30 Sep, 2026 · Python 3.14

The ticket in Variables was a string. Almost everything sent to or from a model is text, so these are the tools you will use most.

Measuring and cleaning a string

Example
ticket = "  My parcel has NOT arrived  "
print(len(ticket))

clean = ticket.strip()
print(clean)
print(len(clean))

len counts characters, spaces included. strip removes the spaces at both ends, which is why the length drops.

ticket.strip() is a method: an action that belongs to a value, called with a dot. It gives back a new string. The original ticket still has its spaces.

Changing case and searching with in

Example
print(clean.lower())
print(clean.upper())
print("arrived" in clean.lower())
print("refund" in clean.lower())

in asks whether one piece of text appears inside another, and answers True or False. Lowering the ticket first means NOT, Not and not all match.

Putting values into text with f-strings

Example
customer = "Ben"
ticket_id = 2
print(f"Hello {customer}, we are looking at ticket {ticket_id}.")

An f before the opening quote makes an f-string. Each {name} inside is replaced by that variable's value, even when the value is a number.

Building a prompt

A prompt is the text you send to a model. Most prompts are a fixed instruction with some data put into it, which is what an f-string does.

Example
ticket = "My parcel has not arrived"
prompt = f"Sort this ticket into billing, shipping or other.\nTicket: {ticket}"
print(prompt)

\n inside a string means a new line. It is two characters in the code and one line break in the output.

f-string vs joining with +

f-string+
Codef"Hello {customer}""Hello " + customer
Numbers insideConverted for youNeed str() first, which turns a number into text
Reading a long promptOne piece of textMany pieces and quotes

Where strings show up in AI code

  • Cleaning what a user typed before it goes into a prompt.
  • Building the prompt itself with an f-string.
  • Checking a model's reply for a word with in.
Watch out. String methods never change the string they are called on. ticket.strip() on its own line does nothing you can see; store the result, as in clean = ticket.strip().
Try it yourself
  • Add clean = "My parcel has NOT arrived" and print clean.replace("parcel", "order").
  • Add customer = "Ben" above the prompt and put {customer} into it as well.
  • Remove the f from the f-string and see what prints instead.
PreviousVariables

This is what real progress feels like.