In-depth explanation of Python programming strings and python strings
Python escape characters
When special characters are needed, python uses a backslash (\) to escape the characters. See the following table:
Python string Operators
The following table lists the instance variable a with the string "Hello" and variable B with the value "Python ":
Python string formatting
Python supports formatting string output. Although this may use a very complex expression, the most basic usage is to insert a value into a string with the string format character % s.
In Python, string formatting uses the same syntax as the sprintf function in C.
Example:
#!/usr/bin/pythonprint "My name is %s and weight is %d kg!" % ('Zara', 21)
Output result of the above instance:
My name is Zara and weight is 21 kg!
Python string formatting symbol:
Auxiliary instructions for formatting operators:
Python three quotes (triple quotes)
You can copy complex strings with three quotation marks in python:
Python three quotes allow a string to span multiple lines. A string can contain line breaks, tabs, and other special characters.
The syntax of the three quotation marks is a pair of consecutive single or double quotation marks (usually used in pairs ).
>>> hi = '''hi there'''>>> hi # repr()'hi\nthere'>>> print hi # str()hi there
The three quotation marks free programmers from the quarary of quotation marks and special strings. The so-called WYSIWYG (WYSIWYG) format keeps a small string from start to end.
A typical use case is that when you need an HTML or SQL statement, character strings can be combined to escape special strings.
errHTML = '''<HTML><HEAD><TITLE>Friends CGI Demo</TITLE></HEAD><BODY><H3>ERROR</H3><B>%s</B><P><FORM><INPUT TYPE=button VALUE=BackONCLICK="window.history.back()"></FORM></BODY></HTML>'''cursor.execute('''CREATE TABLE users ( login VARCHAR(8), uid INTEGER,prid INTEGER)''')
Unicode string
Defining a Unicode string in Python is as simple as defining a normal string:
>>> u'Hello World !'u'Hello World !'
The "u" in lower case before quotation marks indicates that a Unicode string is created here. If you want to add a special character, you can use the Unicode-Escape encoding of Python. For example:
>>> u'Hello\u0020World !'u'Hello World !'
The replaced \ u0020 mark indicates that a Unicode character (space character) with an encoding value of 0x0020 is inserted at a given position ).