Python008 Python3 string, python008python3
var1 = 'Hello World!'var2 = "QQ603374730"
Value in the Python access string
Python does not support the single character type. A single character is also used as a string in Python.
Python can use square brackets to intercept a substring, as shown in the following example:
Instance (Python 3.0 +)
#!/usr/bin/python3var1 = 'Hello World!'var2 = "QQ603374730"print ("var1[0]: ", var1[0])print ("var2[1:5]: ", var2[1:5])
Execution result of the above instance:
var1[0]: Hvar2[1:5]: Q603
Python string update
You can modify an existing string and assign it to another variable, as shown in the following example:
Instance (Python 3.0 +)
#! /Usr/bin/python3var1 = 'Hello World! 'Print ("updated string:", var1 [: 6] + 'qq603374730! ')
Execution result of the above instance
Updated string: Hello QQ603374730!
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 ":
Instance (Python 3.0 +)
#! /Usr/bin/python3a = "Hello" B = "Python" print ("a + B output result:", a + B) print ("a * 2 output result: ", a * 2) print (" a [1] output result: ", a [1]) print (" a [] output result: ", a []) if ("H" in a): print ("H in variable a") else: print ("H is not in variable a") if ("M" not in): print ("M not in variable a") else: print ("M in variable a") print (R' \ n ')
The output result of the above instance is:
Output result of a + B: HelloPythona * 2 output result: HelloHelloa [1] output result: ea [] output result: ellH in variable a M is not in variable a \ n
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.
Instance (Python 3.0 +)
#! /Usr/bin/python3print ("My name is % s. % d years old! "% ('Xiaoming ', 10 ))
Output result of the above instance:
My name is James, 10 years old!
Python string formatting symbol:
Auxiliary instructions for formatting operators:
In Python2.6, a new function str. format () is added, which enhances the string Formatting Function.
Python three quotes
Python three quotes allow a string to span multiple lines. A string can contain line breaks, tabs, and other special characters. The instance is as follows:
Instance (Python 3.0 +)
#! /Usr/bin/python3para_str = "is an instance of multi-line strings. You can use the TAB (\ t) for multi-line strings ). You can also use the line break [\ n]. "Print (para_str)
The execution result of the above instance is:
This is an instance of multi-line strings. You can use the TAB () for multi-line strings (). You can also use the line break [].
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
In Python2, common strings are stored in 8-bit ASCII code, while Unicode strings are stored as 16-bit unicode strings, which can represent more character sets. The syntax used is to prefix the string with u.
In Python3, all strings are Unicode strings.
Python string built-in functions
Common built-in functions of Python strings are as follows:
Python3 Number (Number) Python3 list
Note list
String truncation character supplement:
#0, a, and B are parameters. Truncates characters from the place where the string pointer is a to the previous position of B (because it does not contain B) var1 = "hello world"; print (var1 [a: B]); #1. If neither a nor B is specified, all characters are used by default. That is, the following two print results are the same print (var1 [:]); # hello worldprint (var1); # hello world #2. If a is filled in, B Does not fill in (or the value entered is greater than the pointer subscript). By default, it is intercepted from a to print (var1 [3:]) at the last position of the string. # lo world #3. If a is left blank and B is left blank, the screenshot is intercepted from the 0 position by default, and the print (var1 [: 8]) from the previous position of B; # hello wo #4. If a is negative, print (var1 [-2:]) is truncated from a position at the end by default. # ld #5. If a> = B, the default output is null. Print (var1 [3: 3]); print (var1 [3: 2]);
The python string formatting symbol % f can specify the precision after the decimal point.
>>> num=18.7254>>> print("the price is %.2f" %num)the price is 18.73>>>