Strings


Written by PIRATE KING

Single, Double, Trible Quotes

Remember, when working with text, always enclose it in quotes—either single '' or double "", depending on your preference. For longer text, you can use triple quotes """ to format multi-line strings. Here's an example.

"""Hello,

This is a multi-line message.

Best,

PIRATE KING"""

After defining a multi-line string, you can assign it to a variable, such as message. When you print(message),

it shows up in the Terminal exactly as it appears in your code—formatting, line breaks, and all. Python triple quotes are often used for writing docstrings, which are simple text notes you put right after defining a function, class, or module in Python. They explain what the code does, making it easier for anyone reading the code to understand. These uses make code more readable and maintainable, especially with longer text or documentation.

String Index

In Python, strings are zero-indexed, which means the first character begins at position 0, not 1. If you want to get a specific character in a string, use square brackets [] with the index number, like this:

Printing index 0 gives 'P'. How about name[5]?

We get 'e' because 'e' is the character at index 5.

You can also count from the end using negative numbers: -1 means the last character, -2 the second last, and so on. So name[-1] gives 'g', the last character, and name[-2] prints 'n', the second last.

String Slicing

String slicing in Python lets you quickly grab chunks of a string using square brackets and the format [start:end]. Let's go over some examples.

Here, text[0:5] will return the first five characters in the text variable: "Hello". Notice how the start index is inclusive but not the end index. What if we omit the starting index? text[:5]

Once again, "Hello" is printed because by default, Python will use 0 as the starting index. Now, what if we omit the end index, like this? text[7:]

It goes all the way to the end, printing the seventh character W and onwards, "World!". What do you think will happen if we do these: text[-6:-1] and text[-6:]?

Python will count from the end of the string, printing World and World! Lastly, we can create a copy of the string by omitting both the start and the end index: text[:]

String slicing is flexible and works with any combination of positive, negative, or omitted indices for grabbing any part of a string efficiently. Just remember to use square brackets [] and the format [start:end]

String Concatenation

last = "Monkey"
middle = "D."
first = "Luffy"

We have three variables: last = "Monkey", middle = "D.", and first = "Luffy". To print the full name "Monkey D. Luffy" using all three, we join them together with string concatenation. String concatenation just means combining strings, and in Python, the + operator lets us do it easily. Let me show you how:

Here, full_name uses the + operator and spaces to glue everything together. When you print(full_name), you get “Monkey D. Luffy” on the console.

Escape Sequences

Let's say you want to explicitly print double quotes ". If you type """, Python thinks the string isn't finished and throws an error.

Escape sequences in Python let you include special characters inside strings that would otherwise be tricky to type or cause errors. They start with a backslash \ followed by a character that represents something else. Here are some common escape sequences with examples:

Escape sequences let you handle quotes, new lines, tabs, and special characters cleanly in strings without confusing Python

Formatted Strings

Formatted strings, or f-strings, in Python let you embed variables and expressions directly inside a string, making your code cleaner and easier to read. You create an f-string by putting an f right before the quotes and wrapping variables or code inside {}. For example,

This prints: Hi, I'm Alice and I'm 30 years old. You can also do math or call functions inside the braces:

This outputs, In 5 years, I'll be 35., and There are 5 characters in Alice. F-strings are simple, powerful, and the preferred way to format strings in Python.

Function

A function is like a mini-program inside your code that does a specific job. You set it up once, give it a name, and then you can use it anytime just by calling its name; print, is one of them. Functions help keep your code neat, avoid repeating yourself, and make fixing or updating things much easier.

len()

Let's learn a new function, len(). len() gets the length of a string — basically, how many characters are in it. Now, some functions take additional inputs called arguments. Let's define a variable called name, set it to "Pirate King", and pass it to len() as an argument. It returns how many characters are in that string.

We get 11 because "Pirate King" has 11 characters.

Quiz

1. What type of quotes can you use in Python to create multi-line strings?

2. Which escape sequence inserts a new line in a Python string?

3. What is the correct way to create an f-string that greets a user stored in the variable name?

4. Given the string: name = "Pirate King", write Python code to:

  • Print the first character of name
  • Print the sixth character of name
  • Print the last character of name
  • Print the second last character of name

Expected output:

P
e
g
n

5. Given the string: text = "Hello, World!", write Python code using slicing to print:

  • The first five characters: Hello
  • Characters from index 7 to the end: World!
  • Characters from the 6th last to the 2nd last: World
  • A copy of the entire string: Hello, World!
© PIRATE KING 2025
GOLDSTONE STUDIO LLC
Privacy Policy