Strings
A string is a sequence of characters. In Python, strings are Unicode by default, and any Unicode character can be part of a string. Strings are represented as characters surrounded by quotation marks. Single or double quotations both work, and strings made with them are equal:
'Here is a string' 'Here is a string' "Here is a string" == 'Here is a string' True
If you want to include quotation marks around a word or words within a string, you need to use one type of quotation marks—single or double—to enclose that word or words and use the other type of quotation marks to enclose the whole string. The following example shows the word is enclosed in double quotation marks and the whole string enclosed in single quotation marks:
'Here "is" a string' 'Here "is" a string'
You enclose multiple-line strings in three sets of double quotation marks as shown in the following example:
a_very_large_phrase = """ Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects. """
With Python strings you can use special characters, each preceded by a backslash. The special characters include \t for tab, \r for carriage return, and \n for newline. These characters are interpreted with special meaning during printing. While these characters are generally useful, they can be inconvenient if you are representing a Windows path:
windows_path = "c:\row\the\boat\now" print(windows_path) ow heoat ow
For such situations, you can use Python’s raw string type, which interprets all characters literally. You signify the raw string type by prefixing the string with an r:
windows_path = r"c:\row\the\boat\now" print(windows_path) c:\row\the\boat\now
As demonstrated in Listing 3.3, there are a number of string helper functions that enable you to deal with different capitalizations.
Listing 3.3 String Helper Functions
captain = "Patrick Tayluer" captain 'Patrick Tayluer' captain.capitalize() 'Patrick tayluer' captain.lower() 'patrick tayluer' captain.upper() 'PATRICK TAYLUER' captain.swapcase() 'pATRICK tAYLUER' captain = 'patrick tayluer' captain.title() 'Patrick Tayluer'
Python 3.6 introduced format strings, or f-strings. You can insert values into f-strings at runtime by using replacement fields, which are delimited by curly braces. You can insert any expression, including variables, into the replacement field. An f-string is prefixed with either an F or an f, as shown in this example:
strings_count = 5 frets_count = 24 f"Noam Pikelny's banjo has {strings_count} strings and {frets_count} frets" 'Noam Pikelny's banjo has 5 strings and 24 frets'
This example shows how to insert a mathematic expression into the replacement field:
a = 12 b = 32 f"{a} times {b} equals {a*b}" '12 times 32 equals 384'
This example shows how to insert items from a list into the replacement field:
players = ["Tony Trischka", "Bill Evans", "Alan Munde"] f"Performances will be held by {players[1]}, {players[0]}, and {players[2]}" 'Performances will be held by Bill Evans, Tony Trischka, and Alan Munde'