3-2. Strings

In Python, strings can indeed be enclosed in either single (' ') or double (" ") quotation marks, and both are functionally equivalent. This flexibility allows you to choose the style that best fits your needs, especially when dealing with strings that contain quotation marks.

Here are a few examples to demonstrate different usages:

If your string contains a single quote, you can use double quotes to avoid needing to escape the single quote, and vice versa.

If you need to use the same type of quotation mark inside the string, you can escape it with a backslash (\):

Assigning a string to a variable is done with the variable name followed by an equal sign and the string:

Multiline Strings

Multi-line strings in Python can be defined using triple quotes, either """ or '''. These are particularly useful for strings that span multiple lines, such as long text blocks, code snippets, or formatted text.

Slicing Strings

Slicing is a powerful feature in Python that allows you to extract a subset of characters from a string using a specified range of indices. The general syntax for slicing is string[start:end], where start is the index of the first character you want to include, and end is the index of the character you want to stop before (not included in the slice).

Here's an example that demonstrates how to slice a string to get characters from position 2 to 5 (not included):

The syntax for slicing can be broken down into three parts:

  • start (optional): The starting index of the slice. The character at this index is included in the slice. If omitted, the slice starts from the beginning of the string.
  • end (optional): The ending index of the slice. The character at this index is not included in the slice. If omitted, the slice goes until the end of the string.
  • step (optional): The step size, or the interval between indices. If omitted, the default step is 1.

You can also specify a step size to skip characters:

Negative indices can be used to count from the end of the string:

String Concatenation

String concatenation in Python allows you to combine two or more strings into one. This can be done using the + operator. Here are some examples to illustrate this concept:

To concatenate two strings, you simply use the + operator:

If you want to add a space between the concatenated strings, you include the space within the quotation marks:

String Format

In Python, we cannot combine strings and numbers like this:

However, strings and numbers can be combined by utilizing f-strings or the format() method.

Using f-strings is an efficient and powerful way to format strings in Python. They allow you to embed expressions inside string literals using curly braces {}. This makes it easy to combine strings and variables, perform calculations, and format values.

To create an f-string, prefix the string with the letter f and use curly braces {} to include variables or expressions:

You also can include variables directly within the curly braces:

The format() method provides another way to format strings, which is particularly useful for creating complex formatted strings. This method allows you to insert values into a string using placeholders defined by curly braces {}.

The simplest use of the format() method involves placing curly braces {} within a string and passing the values you want to insert as arguments to the format() method.

You can use positional arguments by placing numbers inside the curly braces, indicating the position of the arguments.