Strings in Python

The string data type can be defined simply as any sequence of characters. In a Python script, such sequences can be represented either by single quotation marks or double quotation marks.

Examples:

phrase1 = 'hard-boiled eggs.'
phrase2 = '"Yes", he replied,'
phrase3 = "I like"
print(phrase2, phrase3, phrase1)

The three variables phrase1, phrase2, and phrase3 are variables of type string.

Notice the use of quotation marks to define a string that itself contains quotes. Also note that the print() function inserts a space between the displayed elements.

The special symbol \ (backslash) allows for additional features:

  • First, it allows writing multi-line statements that would otherwise be too long for a single line (this applies to any type of instruction).
  • Within a string, it is used to insert special characters (new line, single quote, double quotes, etc.).

Examples:

txt3 = '"Isn\'t it?" she replied.'
print(txt3)

Salut = "This is a rather long string\n containing several lines \
    of text (This works\n in the same way in C/C++.\n\
    Note that leading spaces\n at the beginning of a line are significant.\n"
print(Salut)

Notes

  • The symbol \n in a string means a line break.
  • The symbol \’ allows inserting a single quote inside a string defined with single quotes, and similarly \” allows inserting double quotes inside a string defined with double quotes.
  • Remember the rules for variable names (you must distinguish between uppercase and lowercase letters).

Triple Quotation

To easily include special or unusual characters in a string without using the backslash symbol, or to include the backslash itself, one can use triple quotes (either triple double quotes or triple single quotes):

a1 = """
    Example of preformatted text, that is to say
    whose indentations and the
    special characters \ ' " are
    preserved without
    any other form of processing."""
print(a1)

Accessing Individual Characters in a String

Strings represent a special case of more general data types called compound types. A compound type is one that groups together a collection of simpler elements into a single object. In the case of a string, it is clear that it is made up of characters themselves.

Depending on the situation, we may want to process the string as a whole, or sometimes a single element, and sometimes a group of characters. The Python programming language allows access to each character in the string individually, as we will see, and this is not very complicated.

Python considers a string to be an object belonging to the sequence class, which groups ordered collections of elements. This simply means that the characters in a string are always arranged in a specific order.

Therefore, each character in the string has a position, which can be identified using an index.

To access a specific character, we write the name of the variable containing the string and place the index number (which represents the position of the character in the string) inside square brackets.

Note: You will later verify that numbering always starts from zero (0), not one. This also applies to characters in a string.

Example:

ch = "Christine"
print(ch[0], ch[3], ch[5])

You can repeat the previous example, this time using one or two non-ASCII characters. Unlike what could happen in some cases with versions of Python before version 3.0, you will get the expected result:

ch = "Christmas in December"
print(ch[1], ch[2], ch[3], ch[4], ch[8], ch[9], ch[10], ch[11], ch[12])

There is no need to worry for now about how Python stores and handles characters in memory. Just know that this technique relies on the international Unicode standard, which can represent any alphabetic character. Therefore, you can mix Latin, Greek, Cyrillic, Arabic, etc., as well as mathematical symbols, in the same string.

We will see in Chapter 10 (see page 129) how to display characters that are not directly accessible from the keyboard.

String Operations

Python includes many functions that perform various operations on strings (uppercase/lowercase conversion, extracting parts of a string, searching for words, etc.).

For now, we note the following:

1. Concatenation (joining strings)

We can combine several small strings to build a larger one. This is called concatenation and is done in Python using the + operator:

a = 'Small fish'
b = ' will become big'
c = a + b
print(c)

petit poisson deviendra grand

2. Length of a string

We can determine the length of a string (i.e., the number of characters) using the len() function:

ch = 'George'
print(len(ch))

This function works even if the string contains special characters:

ch = 'Ahmed'
print(len(ch))

Converting a string to a number

We cannot add an integer to a string.

ch = '8647'
print(ch + 45)

See this code give an error. But, we can convert a string that represents a number into a numeric value:

ch = '8647'
n = int(ch)
print(n + 65)

In this example, the function int() converts the string into an integer. It is also possible to convert a string into a real number using float().

In the next article we will read about Variables in python.

No Comments

Leave a Reply