Strings and f-strings
Clean up messy text, slice an ID, and build a prompt with an f-string, the same moves behind every prompt template and tool argument.
Most of what flows through an AI system is text: the prompt you send, the arguments a model picks for a tool, the answer that comes back. In Python, text is a string (type str), written inside single or double quotes. Strings are immutable: nothing can change a string once it exists. Every method that looks like it edits a string, such as strip() or upper(), actually returns a new one.
Three skills cover most string work. Indexing and slicing pick out characters by position: positions start at 0, negative positions count from the end, and a slice like s[2:5] includes the start but excludes the end. Methods such as strip(), title(), split() and startswith() clean and inspect text. f-strings build new text from variables: write f before the quote, and anything inside curly braces is evaluated and inserted.
Press Run to execute prompt.py. It takes a messy customer name and a UPI reference, tidies them, and assembles a prompt for a support agent.
# Build a prompt for a support agent from pieces of text.
customer = " priya nair "
order_id = "UPI-2024-7781"
amount = 1249.5
language = "English"
name = customer.strip().title()
first = name.split()[0]
ref = order_id[-4:]
print(name, "|", first, "|", ref)
print(len(name), order_id.startswith("UPI"), "nair" in customer)
prompt = f"You are a support agent. Reply in {language}."
prompt = prompt + f" Customer {first} paid Rs {amount:.2f} (ref ...{ref})."
print(prompt)
print(f"{amount=}")Press Play to run the program line by line, or step with ← →.
Variables
Nothing defined yet.
Output
Nothing printed yet.
What to notice
- Methods chain and never change the original.
customer.strip().title()runsstrip()oncustomer, thentitle()on the result.customerstill holds the spaces afterwards. - Count from zero, and from the end with minus.
name.split()[0]is the first word.order_id[-4:]is the last four characters. The slice end is always excluded, sos[:i] + s[i:]is always the whole string. - An f-string evaluates what is in the braces.
{language}becomes the variable’s value, and{amount:.2f}adds a format spec: a float with two decimals. The part after the colon controls width, precision and padding. +joins strings, and only strings.prompt + f"..."works."Rs " + amountwould fail, becauseamountis a float; put it inside an f-string instead.- Comparison helpers return bools.
startswith(),inand==all giveTrueorFalse, ready for anif.
| Method | What it returns |
|---|---|
s.strip() | s without leading and trailing whitespace |
s.lower(), s.upper(), s.title() | a re-cased copy |
s.split() | a list of words (split on whitespace, or on the separator you pass) |
sep.join(items) | one string made from a list of strings |
s.replace(old, new) | a copy with every old swapped for new |
s.startswith(x), s.endswith(x) | True or False |
Why this matters for AI work
A prompt template is an f-string with the user’s details filled in. A tool’s text arguments arrive exactly as the model wrote them, often with stray spaces or odd capitalisation, so strip() and lower() run before any lookup. And when you print a float into a message for a customer, :.2f is the difference between Rs 1249.5 and Rs 1249.50.