Page Contents (hide)
Strings are very common and fundamental in most programming languages. In Python, strings are a built-in type and categorized as an immutable ordered sequence of characters.
Python strings are objects of the built-in str
class. In an earlier chapter you learned that string literals are represented as a literal sequence of characters enclosed within a pair of either single ('
) or double quotes ("
).
'string' "string's"
When a literal string appears in your program, a str
object is automatically created. Thus, the statement
creates an object storing the given literal string and its reference is assigned to name
. You can also create a string using the str()
constructor
The string constructor can also be used to create string representations of numeric and boolean values.
Likewise, the numeric type constructors can be used to convert numeric strings to the respective type. An exception is raised if the string does not contain a valid literal numeric value.
Strings can also be created via various string operations and methods which are presented in later sections.
Escape sequences are used in Python as they are in Java to represent or encode special characters such as newlines, tabs and even the quote characters. Consider the following example
which produces
Start a newline here. using the \n character.
The common escape sequences are shown in the following table
Sequence | Description |
\\ | Backslash (produces \ ) |
\' | Single quote (produces ' ) |
\" | Double quote (produces " ) |
\n | Newline (produces a newline) |
\t | Horizontal tab |
In addition to literal strings using single or double quotes, Python also has a literal block string in which in which white space (spaces and newlines) is maintained without the need for newline characters or string concatenation. A pair of triple quotes is used to represent a block string as illustrated in the following example
"""This is a string which can continue onto a new line. When printed, it will appear exactly as written between the trip quotes. """
When the string object is created for this literal, Python inserts newline characters (\n
) at the end of each line of the text block and adds blank spaces everywhere they appear within the literal, including at the front of each line. Single or double quotes can be used with the tripple quote representation
'''Here is another multiline string example using triple single quotes.'''
Python provides a number of basic operations that can be performed on strings. Some of these are equivalent to operations in Java, but others are advanced features of Python. A complete list of the string methods and operators are described in the appendix.
In Java, two strings can be concatenated using the plus operator as illustrated below
The same can be done in Python
A major difference however, is that string concatenation can only be done with strings and not other data types. To append a numeric value to the end of a string, you must first create a string representation using the str()
constructor.
Two strings literals can also be concatenated by placing them adjacent to each other
In Java, the length of a string was obtained using the length()
method
Python provides the built-in len()
function that is used to get the length of a string
To access an individual character within a string, Java provided the charAt()
method. The following example extracts and prints the first and last character of a string
In Python, we use an array subscript with the first character having an index of zero. The following illustrates an equivalent Python code segment for the Java code above
Python allows you to index from the end instead of the front by using negative subscripts. The last statement in th previous code segment could be rewritten as follows
The following figure illustrates the front and back index references using the name
string created in an earlier example
If you attempt to access an element of the string that is out of range [0..len(s)] or [-len(s)..−1] an exception will be raised.
In Java, you were able to extract a substring from a string object using the substring()
method of the String class
Python provides the slicing operator for extracting a substring. The following example is the Python equivalent of the previous example
You can also slice a string from the end using a negative index. The following statement
extracts the substring “John S” from the string name
and creates a new string which is assigned to end
.
Python provides a string operation not found in Java for duplicating or repeating a string. Printing a dashed line is a common operation in text-based applications. One way to do it is as a string literal
or we could do it the easy way and use the repeat operator
which produces the same results. When applied to a string and an integer value, the *
operator is treated as the Python repeat operator.
Python overloads the binary and modulus operator (%
) to work with strings. When applied to a string, it creates a new formatted string similar to the printf()
method in Java and the sprintf()
function in C. Consider the following example
which creates a new string using the format definition string from the left side of the %
operator and replacing the format specifier (%5.2f
) with the value of the avgGrade
variable. The resulting string is then printed to standard output. The more common style is to combine these two statements
which is then equivalent to Java’s printf()
method
The formal description for the string format operator is shown below and consists of two parts: the format definition string and the values used to replace the format codes within that definition
format-definition-string % replacement-value(s)
If more than one format specifier is used in the format definition, the replacement values must be placed within parentheses and separated with a comma
The general structure of a format specifier is
%[flags][width][.precision]code
where
0
) which fills preceding blank spaces within the field with 0 and optional justification within the given field width: +
for right-justification or -
for left-justification.
Code | Description |
%s | String (or any object) |
%c | Character (from an ASCII value) |
%d | Decimal or integer value |
%i | Integer value (same as %d ) |
%u | Unsigned integer |
%o | Octal integer |
%x | Hexadecimal integer |
%X | Same as %x but uppercase |
%e | Floating-point with exponent |
%E | Same as %e but uppercase |
%f | Floating-point no exponent |
%g | Same as %e or %f |
%G | Same as %g but uppercase |
%% | Prints a literal % |
The following sample program, which is a modified version of the wages.py program, illustrates the use of formatted strings.