str1 = 'CCCP' str2 = "CCCP" print(str1) # Prints 'CCCP' print(str2) # Prints 'CCCP'
# Use a backslash to include quotes inside of a string. single_quote = 'This one has \'quotes\'' double_quote = "This one also has \"quotes\"" print(single_quote) print(double_quote)
# Use triple quotes to include text that spans multiple lines. wcwilliams = """And you may be sure not one leaf will lift itself from the ground and become fast to a twig again.""" print(wcwilliams)
A string is a sequence of characters. Python's built-in string class includes
methods for searching strings, transforming strings, and checking to see if a
string has particular characteristics. Strings are defined inside either double
quotes ("ABC") or single quotes ('ABC').
Because strings are defined between quotation marks, to include such marks within the string itself you must use the \ (backslash) character. (See the second example above.) This is known as an escape sequence. Other escape sequences include \t for the tab character and \n for new line. Because backslash is the escape character, to include a single backslash within a string, you must use two consecutive backslashes, as in: \\
Normally, strings in your program must be written entirely on one line. If you want to include a chunk of text in your program that spans multiple lines, you can use triple quotes (either """ or '''). (See the third example above.)
There are more string methods than those linked from this page. Additional documentation is located online in the official Python documentation.
'str' "str" """ str """