--- jupyter: jupytext: text_representation: extension: .Rmd format_name: rmarkdown format_version: '1.2' jupytext_version: 1.11.5 kernelspec: display_name: Python 3 (ipykernel) language: python name: python3 orphan: true --- # String literals in Python A string literal is where you specify the contents of a string in a program. ```{python} a = 'A string' ``` Here 'A string' is a string literal. The variable `a` is a string variable, or, better put in Python, a variable that points to a string. String literals can use single or double quote delimiters. ```{python} a = 'A string' # string literal with single quotes b = "A string" # string literal with double quotes b == a # there is no difference between these strings ``` Literal strings with single quote delimiters can use double quotes inside them without any extra work. ```{python} print('Single quoted string with " is no problem') ``` If you need an actual single quote character inside a literal string delimited by single quotes, you can use the backslash character before the single quote, to tell Python not to terminate the string: ```{python} print('Single quoted string containing \' is OK with backslash') ``` Likewise for double quotes: ```{python} print("Double quoted string with ' is no problem") print("Double quoted string containing \" is OK with backslash") ``` Some characters preceded by a backslash have special meaning. For example: ```{python} print('Backslash before "n", as in \n, inserts a new line character') ``` If you do not want the backslash to have this special meaning, prefix your string literal with 'r', meaning "raw": ```{python} print(r'Prefixed by "r" the \n no longer inserts a new line') ``` You can use triple quotes to enclose strings with more than one line: ```{python} print('''This string literal has more than one line''') ``` Triple quotes can use single or double quote marks: ```{python} print("""This string literal also has more than one line""") ```