See All Titles |
![]() ![]() Summary of String HighlightsConsists of Characters Delimited by Quotation MarksYou can think of a string as a Python data type which you can consider as an array or contiguous set of characters between any pair of Python quotation symbols, or quotes. The two most common quote symbols for Python are the single quote, a single forward apostrophe ( ' ), and the double quotation mark ( " ). The actual string itself consists entirely of those characters in between and not the quote marks themselves. Having the choice between two different quotation marks is advantageous because it allows one type of quote to serve as a string delimiter while the other can be used as characters within the string without the need for special escape characters. Strings enclosed in single quotes may contain double quotes as characters and vice versa: >>> quote1 = 'George said, "Good day Madam. How are we today?"' >>> print quote1 George said, "Good day Madam. How are we today?" >>> quote2 = "Martha replies, 'We are fine, thank you.'" >>> print quote2 Martha replies, 'We are fine, thank you.' Python Does not Support a Separate Character TypeStrings are the only literal sequence type, a sequence of characters. However, characters are not a type, so strings are the lowest-level primitive for character storage and manipulation. Most applications tend to deal with strings as a whole and singular entity. To that end, Python provides a good amount of string utilities in the form of operators, built-in functions, and the contents of the string module. However, Python is flexible, allowing access to individual or groups of characters, if desired. Also see Section 6.7.1. Characters are simply strings of length one. String Format Operator ( % ) Provides printf()-like FunctionalityIn Section 6.4.1, we highlighted the printf()-like string format operator which provides a familiar interface to formatting data for output, whether to the screen or elsewhere. Triple QuotesIn Section 6.7.2, we introduced the notion of triple quotes, which are strings that can have special embedded characters like NEWLINEs and TABs. Triple-quoted strings are delimited by pairs of three single (' ' ') or double (""") quotation marks. Raw Strings Allow for Special Characters to be Taken VerbatimIn Section 6.4.2, we introduced raw strings and discussed how they do not interpret special characters escaped with the backslash. This makes raw strings ideal for situations where strings must be taken verbatim, for example, when describing regular expressions. Unlike C strings, Python strings do not Terminate with NUL or '\0'One of the problems in C is running off the end of your string into memory that does not belong to you. This occurs when strings in C are not properly terminated with the NUL or '\0' character, which has the ASCII value of zero. Along with managing memory for you, Python also removes this little burden or annoyance. Strings in Python do not terminate with NUL, and you do not have to worry about adding them on. Strings consist entirely of the characters that were designated and nothing more.
|
© 2002, O'Reilly & Associates, Inc. |