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
ticket = " My parcel has NOT arrived "
print(len(ticket))
clean = ticket.strip()
print(clean)
print(len(clean))29 My parcel has NOT arrived 25
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
print(clean.lower())
print(clean.upper())
print("arrived" in clean.lower())
print("refund" in clean.lower())my parcel has not arrived MY PARCEL HAS NOT ARRIVED True False
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
customer = "Ben"
ticket_id = 2
print(f"Hello {customer}, we are looking at ticket {ticket_id}.")Hello Ben, we are looking at ticket 2.
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.
ticket = "My parcel has not arrived"
prompt = f"Sort this ticket into billing, shipping or other.\nTicket: {ticket}"
print(prompt)Sort this ticket into billing, shipping or other. Ticket: My parcel has not arrived
\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 | + | |
|---|---|---|
| Code | f"Hello {customer}" | "Hello " + customer |
| Numbers inside | Converted for you | Need str() first, which turns a number into text |
| Reading a long prompt | One piece of text | Many 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.
ticket.strip() on its own line does nothing you can see; store the result, as in clean = ticket.strip().Related
- Previous: Variables
- Next: Numbers and type conversion
- Reference: String methods in the Python docs
- Add
clean = "My parcel has NOT arrived"and printclean.replace("parcel", "order"). - Add
customer = "Ben"above the prompt and put{customer}into it as well. - Remove the
ffrom the f-string and see what prints instead.
This is what real progress feels like.