Python Strings: Immutability, Length, and Escape Characters

In Python programming, strings are fundamental building blocks for handling text. Let's delve into a concise exploration of Python strings and their unique characteristics.

Python strings, at their core, are immutable sequences. This means once a string is created, its contents remain unchangeable. This inherent immutability ensures that strings exhibit predictable and recognizable behavior, enhancing your programming experience.

Consider the following code snippets for a closer look at string properties:

Calculating Length

The len() function provides the character count within a string. For instance, executing the code snippet below results in an output of 5.

word = 'short'
print(len(word))

OUTPUT:

Empty Strings

An empty string, indicated by '', possesses a length of 0. This is shown in the example below.

empty = ''
print(len(empty))

OUTPUT:

Escape Character Impact

Notably, the backslash \ acts as an escape character, modifying character interpretations. However, such escape characters do not contribute to the string's length. For instance, the example shown below showcases a length of 4 despite the presence of an escape character.

its = 'it\'s'
print (len(its))

OUTPUT:

These examples shed light on string behavior, emphasizing immutability, length considerations, and the treatment of escape characters. By running and examining these code snippets, you can solidify your understanding of Python strings.

Multiline Strings

Python provides a clever solution for handling text that spans multiple lines in your code. This is particularly useful when you want to work with longer strings without the limitations of the regular single-line syntax. Let's explore this feature and understand how it can make your code more readable and efficient.

The Issue with Single-Line Strings

In Python, you've likely become familiar with creating strings using single quotes or double quotes, like this:

message = 'Hello, world!'

However, this method becomes cumbersome when you need to define a string that occupies multiple lines. Trying to extend a string beyond one line using single or double quotes results in an error:

# This is incorrect and will cause an error
multiline = 'Line #1
Line #2'

The Triple Quotes Solution

Python comes to the rescue with a cleaner and error-free way to define multiline strings: triple quotes. Instead of using single or double quotes, you can use triple single quotes (''') or triple double quotes (""") to define multiline strings. Here's how it works:

# Using triple single quotes
multiline = '''Line #1
Line #2'''

# Or using triple double quotes
multiline = """Line #1
Line #2"""

Invisible Whitespace: The Line Feed

When you use multiline strings, something interesting happens behind the scenes. The string might look like it has more characters than you expect, but that's because an invisible character called a "line feed" (\n) is added to the end of each line. This line feed character is used to indicate a new line, and even though you can't see it, it's counted as part of the string's length.

For instance, consider the following multiline string:

multiline = '''Line #1
Line #2'''

Even though there are only 14 visible characters, the string's length is actually 15 due to the presence of the line feed between "Line #1" and "Line #2".

Operations on Strings

Strings can be operated on in a variety of ways, including:

1. Concatenation: Strings can be concatenated (joined together) using the + operator. For example, the following code would concatenate the strings "Hello" and "World":

str1 = "Hello"
str2 = "World"

print(str1 + str2)

OUTPUT:

2. Replication: Strings can be replicated using the * operator. For example, the following code would replicate the string "a" five times:

str = "a"

print(5 * str)

OUTPUT:

3. Conversion to code points: The ord() function can be used to convert a character to its ASCII/Unicode code point value. For example, the following code would convert the character "a" to its code point value:

char = "a"

print(ord(char))

This code would output the integer 97, which is the ASCII code point value for the character "a".

OUTPUT:

4. Conversion from code points: The chr() function can be used to convert a code point to its corresponding character. For example, the following code would convert the code point value 97 to the character "a":

code_point = 97

print(chr(code_point))

OUTPUT:

Strings as Sequences

Strings in Python are sequences, which means that they can be indexed and iterated over. This means that you can access individual characters in a string by their position, and you can loop through a string one character at a time.

For example, the following code would print the characters in the string "I walk alone" one by one:

the_string = "I walk alone"

for character in the_string:
    print(character)

OUTPUT:

You can also use negative indices to access characters from the end of the string. For example, the following code would print the last character in the string "I walk alone":

the_string = "I walk alone"

print(the_string[-1])

OUTPUT:

Iterating through a string is also possible. The following code would print the characters in the string "I walk alone" one by one, along with their index:

the_string = "I walk alone"

for i in range(len(the_string)):
    print(i, the_string[i])

OUTPUT:

Slices

Slices are a powerful tool for accessing parts of a string. They are similar to indexing, but they allow you to specify a range of characters to be included.

The syntax for slices is:

  • string[start:stop:step]
  • start is the index of the first character to include.
  • stop is the index of the character after the last character to include.
  • step is the number of characters to skip between each character included.

If start is omitted, it defaults to 0. If stop is omitted, it defaults to the length of the string. If step is omitted, it defaults to 1.

For example, the following code would print the characters from the second to the fourth in the string "abdefg":

alpha = "abdefg"

print(alpha[1:4])

OUTPUT:

The step argument can also be used to skip characters. For example, the following code would print the even-numbered characters in the string "abdefg":

alpha = "abdefg"

print(alpha[::2])

OUTPUT:

Here are some additional things to keep in mind about slices:

  • Slices can be used to access a range of characters from the end of a string as well. For example, the following code would print the last three characters in the string "abdefg":
alpha = "abdefg"

print(alpha[-3:])

OUTPUT:
  • Slices can be used to access the entire string. For example, the following code would print the entire string "abdefg":
alpha = "abdefg"

print(alpha[:])

OUTPUT:
  • Slices can be used to create new strings. For example, the following code would create a new string containing the even-numbered characters in the string "abdefg":
alpha = "abdefg"

new_string = alpha[::2]

print(new_string)

OUTPUT:

The in and not in operators

The in and not in operators are used to check if a value is contained in a sequence. In the context of strings, this means that they can be used to check if a substring is contained in a string.

The in operator returns True if the value is contained in the sequence, and False otherwise. The not in operator returns the opposite: True if the value is not contained in the sequence, and False otherwise.

For example, the following code would print True:

string = "This is a string."
substring = "is"
print(substring in string)

This code would print False:

string = "This is a string."
substring = "string."
print(substring in string)

The not in operator can also be used to check if a value is not contained in a sequence. For example, the following code would print True:

string = "This is a string."
substring = "not in this string."
print(substring not in string)

This code would print False:

string = "This is a string."
substring = "string"
print(substring not in string)

Python Strings are Immutable

In Python, strings are immutable. This means that they cannot be changed once they are created. This is in contrast to lists, which are mutable and can be changed.

The immutability of strings has a few important implications:

  • You cannot use the del statement to remove characters from a string.
  • You cannot use the append() or insert() methods to add or insert characters into a string.
  • If you want to change a string, you must create a new string with the desired changes.

While the immutability of strings may seem limiting at first, it actually makes strings more efficient. When you change a list, you are actually modifying the list in place. This can lead to performance problems if the list is large. When you change a string, you are creating a new string with the desired changes. This is more efficient, as it does not require any changes to the original string.

Here is an example of how to change a string in Python:

original_string = "This is a string."
new_string = original_string + " and it's immutable."
print(new_string)

This code first creates the string original_string. Then, it creates a new string new_string by adding the text "and it's immutable." to the end of original_string. Finally, it prints the value of new_string.

The output of this code is:

OUTPUT:

As you can see, the original string was not changed. Instead, a new string was created with the desired changes. This is because strings are immutable in Python.

More String Operations

In the previous part of the article, we discussed some of the basic operations that can be performed on strings in Python. In this part, we will discuss some more advanced operations, including:

  • Finding the minimum and maximum characters in a string using the min() and max() functions.
  • Finding the index of the first occurrence of a character in a string using the index() method.
  • Creating a new list from the characters in a string using the list() function.
  • Counting the number of occurrences of a character in a string using the count() method.

Let's take a look at some examples of how these functions can be used.


Finding the minimum and maximum characters in a string

The min() and max() functions can be used to find the minimum and maximum characters in a string. The min() function returns the character with the lowest ASCII value, and the max() function returns the character with the highest ASCII value.

For example, the following code finds the minimum and maximum characters in the string "aAbByYzZ":

string = "aAbByYzZ"

minimum_character = min(string)
maximum_character = max(string)

print("The minimum character is:", minimum_character)
print("The maximum character is:", maximum_character)

OUTPUT:

Finding the index of the first occurrence of a character in a string

The index() method can be used to find the index of the first occurrence of a character in a string. The index is the position of the character in the string, starting from 0. If the character does not occur in the string, the index() method will raise a ValueError exception.

For example, the following code finds the index of the first occurrence of the letter b in the string "abcabc":

string = "abcabc"
index_of_b = string.index("b")
print("The index of the first 'b' is:", index_of_b)

OUTPUT:

Creating a new list from the characters in a string

The list() function can be used to create a new list from the characters in a string. The new list will have one element for each character in the string.

For example, the following code creates a new list from the characters in the string "abcabc":

string = "abcabc"
new_list = list(string)
print(new_list)

OUTPUT:

Counting the number of occurrences of a character in a string

The count() method can be used to count the number of occurrences of a character in a string.

For example, the following code counts the number of occurrences of the letter b in the string "abcabc":

string = "abcabc"
number_of_b = string.count("b")
print("The number of 'b's in the string is:", number_of_b)

OUTPUT:

Summary

In this article, we learned about the basics of strings in Python. We learned that:

  • Strings are immutable sequences of characters.
  • There are two types of strings in Python: one-line strings and multi-line strings.
  • The length of a string is determined by the len() function.
  • Strings can be concatenated using the + operator and replicated using the * operator.
  • The chr() and ord() functions can be used to create a character using its codepoint, and to determine a codepoint corresponding to a character.
  • Some other functions that can be applied to strings are: list(), max(), and min().
  • The index() method finds the index of a given substring inside the string.

Here are some additional things to keep in mind about strings in Python:

  • Strings are case-sensitive.
  • Backslashes can be used to escape special characters, such as newlines and quotes.
  • Strings can be used to represent numbers, dates, and other data types.
  • Strings can be used to format output and to create user interfaces.