Other common methods of strings:
Ord (char) # returns the code value corresponding to the char character, which can be a Chinese character
Chr (x) # Enter a Unicode code to return the corresponding character
eval (str) # returns the contents of Str in the form of code
REPR (x) returns the object's str format, suitable for machine execution
String formatting
% formatting:
%[flags][width][.precision]typecode
Format formatted:
[Fill] [Align] [Sign] [#] [Width] [. Precision] [Type]
Fill: Filling symbol
Align: alignment < Align Left,> right, ^ center align
Sign: Display symbol, + display plus sign,-display symbol
#: 0b,0o,0x is displayed for 2,4,8
Width: Wide
, thousands of bits
. Precision: Accuracy
Type:s string D shaping f floating-point type
Bytes:
Create bytes:
B1 = B ' Hello ' # Add a B before the string
B2 = Byte ("Chinese", ' utf-8 ')
To set the encoding format:
#-*-Coding:utf-8-*-
Import Sys
Sys.setdefaultencoding (' Utf-8 ') #修改系统默认编码格式
String to byte: ' Chinese '. Endcode (' Utf-8 ')
byte-to-string: B ' \xe4\xb8\xad\xe6\x96\x87 '. Decode (' Utf-8 ')
20 Basics of Python programming