When creating strings, you can use single, double, or triple quotes, taking care to ensure that the string is enclosed in a pair of matching quotes. A string can contain one or more quotation marks of a different variety (which might be useful if you are enclosing quoted text within a larger text string, or are using the single quote as an apostrophe mark). Strings enclosed in triple quotes (""" or ''') can also be block strings: they will encode newline characters if the string is entered over multiple lines. In addition, they are conventionally used to create docstrings (documentation strings) within source code. For example:

Even though a Python string-type variable is immutable once created, the language provides plenty of convenient methods for arranging and combining strings,, so you can ultimately get the output you want.

String concatenation

As we have seen, Python allows concatenation of string variables or constants using the + operator. For example, if a and b are string variables, they may be joined using the plus sign like this:

Because we have not assigned the expression a + b to a variable, the resulting string is echoed within the interpreter, since it represents the result of the expression. If we wish to insert a space between the strings and assign the result to a new variable, we must take care of it explicitly:

or:

Notice that any blank spaces outside of the quotes are irrelevant to these operations. Python ignores them.

As you might imagine, string constants may be concatenated just like string variables:

String replication

It may or may not surprise you to learn that while Python does not allow you to add a string to a number, you can multiply a string by a number. If the number is N, the resulting string just replicates the original string N times.

This trick only works for integers; it fails for floats. Deciding what sorts of functions to attach to operators such as + and * is a tricky part of language and/or library design. The Python developers have decided that string replication via multiplication by an integer makes intuitive sense, while multiplication by a float does not. On the other hand, the intended consequence of addition of an integer to a string is not obvious (and if the intended result is to convert the int to a string and concatenate it with the existing string, then that conversion operation must be explicitly provided, as described below).

The str() function

What if you want to concatenate a string with a number? You quickly find the naive approach doesn’t work:

Use the str() function first to convert the variable or expression into a string. Then string operators like + can be applied to it:

In Python, str is not just the name of a function: it stands for a whole class, i.e., it is the formal type of any string object. Thus, 'Joe' is an object of class str. The str() constructor takes an arbitrary object as input and creates a str object from it. In this sense it is similar to some of the other constructors that we have examined in the context of type conversions, such as int() and float(). In all these cases, the name of the class is both the name of the formal data type and the name of the constructor, i.e., the function that is used to create objects of that particular type. But str() is somewhat special, because any Python object can be converted to some sort of string representation (even if it just contains minimal information about the object), while not every object can be converted to an int or a float.

 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement