Skip to main content

What are strings?

Strings are text - any characters inside quotes. Python doesn’t care if you use single or double quotes, just be consistent.

Creating strings

Three ways to make strings:
Use double quotes when your text contains apostrophes: "It's Python!"

Combining strings

Join strings together with +:

String length

Use len() to count characters:

Converting to string

Turn other types into strings with str():

Adjacent String Literals

Python automatically concatenates adjacent string literals into a single string.

Example

This is equivalent to:

Important

Spaces are not added automatically.
So, when splitting a string across lines, include spaces explicitly:
Result:
This is commonly useful for writing long prompts:

String .format() Method

The .format() method replaces {placeholders} in a string with actual values. This is useful for creating dynamic prompt templates.

Basic Prompt Example

Output:

Multiple Variables

A prompt can contain multiple placeholders:
Output:

RAG-Style Prompt

A common RAG prompt contains both context and a question:
The placeholders are replaced with the actual values.

Why This Matters in LangChain

LangChain’s prompt templates use the same {variable} style:
Later, LangChain supplies the values:
Conceptually:
.format() is Python’s string formatting mechanism. ChatPromptTemplate provides a similar placeholder-based mechanism specifically for building prompts.

Common mistakes

What’s next?

Let’s explore True/False values - essential for making decisions in your code!

Booleans

True and False values