Strings


Written by PIRATE KING

Starting the video will automatically play it from this chapter.

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. Try assigning the example above to a variable yourself.

Exercise

Instruction:

Copy the triple-quoted string (including the quotes) from the example above and assign it to the variable message, then display it using the print() function.

Expected output:

Hello,

This is a multi-line message.

Best,

PIRATE KING

Copy the triple-quoted string (including the quotes) from the example above and paste it after 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 you can easily access characters starting from the end of the string.

Exercise

Instruction:

Use indices -1 and -2 to print the last and second last characters respectively.

Expected output:

P
e
g
n

Negative indexing allows you to count from the end of the string. Try using name[-1] for the last character.

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:]?

Exercise

Instruction:

Use slicing on the variable text to print the string from index -6 onwards to the terminal.

Expected output:

World
World!

Recall that leaving the second index in the slice [start:] empty tells Python to extract characters from the start index to the end of the string.

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.

Exercise

Instruction:

Create a new variable called full_name that will use string concatenation to print "Monkey D. Luffy" using the three provided variables. Use the + operator to join variables and strings together.

Expected output:

Monkey D. Luffy

You can create spaces in between strings using " "

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.

Exercise

Instruction:

Complete the program to match the expected output. Use an f-string and the expression age + 5 to print the age in 5 years.

Expected output:

Hi, I'm Alice and I'm 30 years old.
In 5 years, I'll be 35.

Place the expressions age + 5 inside curly braces {}.

This outputs In 5 years, I'll be 35.. 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.

Exercise

Instruction:

Use the len() function to print the length of the string stored in the variable name.

Expected output:

11

Pass the variable name as an argument to the len() function, then wrap that call inside print() to display the result.

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

Positive indices start from 0. To count from the end, use negative indices starting from -1.

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!

Expected output:

Hello
World!
World
Hello, World!

Use the [start:end] syntax. For example, text[0:5] gets the first five characters. Negative indices like -6 also work.

© PIRATE KING 2026
GOLDSTONE STUDIO LLC
Privacy Policy