String * * *
is an ordered sequence of characters, a collection of characters.
Use single quotes, double-direction quotes, and three-quote-quoted character sequences #三引号中可以放sql语句
A string is an immutable object
string element Access
String support using indexed access
A sequence of character sets, sequences of characters
A string can be a container that can be iterated over, because it is sequential
String Join connection *
"String". Join (iterable), str
Concatenate an iterative object, using string as a delimiter
An iterative object itself is a string of elements
Returns a new string
# ', '. Join () #join后接字符串, "You can customize the spacer
# ', '. Join (Map (Str,range (10)))
string + connection #+ str
#将2个字符串连接在一起
#字符串相加产生新的字符串返回值为none
String Splitting *
#分割字符串的方法分为2类
#split系: Splits a string into several strings by delimiter and returns the list
#partition系: Splits a string into 2 segments, returning the tuple of 2 segments and separators
Split (Sep=none, Maxsplit=-1), List of strings #从左向右
#sep可以改分隔符默认是空白字串作为分隔符
# MAXSPLIT Specifies the number of splits, 1 means traversing the entire string
# #返回类型是什么很重要 Decide if I could join
#rsplit () #从右向左切, use the same
Splitlines ([keepends]), List of strings
#按行切一行行切
#keepends refers to whether row separators are preserved
# line delimiter includes \ n, \ r \ r, etc.
#常见换行符:/r,/n,/r/n
#空白分隔符: Spaces,/t,/n
Partition (Sep) (head, Sep, tail)
#可以split的一个特例只切一回, delimited, but the tuple is returned after the cut
# from left to right, encounter the delimiter to split the string into two parts, return the header, delimiter, tail three parts of the ternary group; If no delimiter is found, returns the ternary group of the header, 2 empty elements
#sep分隔符字符串必须指定, you cannot use null characters
Tangent strings are more commonly used: for example, in branches, file path paths, telephone area codes, and so on.
String case
Upper () #全大写
Lower () #全小写
#大小写, use it when you make judgments.
Swapcase () #交互大小写
String typesetting
Title () Str #标题的每个单词都大写
Capitalize () Str #首个单词大写
Center (width[, Fillchar]) str
#width Print Width
#fillchar-populated characters
#格式: Example: A.center (20, ' @ ')
Zfill (width), str
#width print width, right, left with 0 padding
Ljust (width[, Fillchar])-Str left justified
Rjust (width[, Fillchar]), str right-justified
#中文用的少, look.
String Modification * * *
(replace)-STR # (replace) replace (old, new[, Count])
#字符串中找到匹配替换为新子串, returns the new string
#count表示替换几次, do not specify is replace all
(delete) strip ([chars]), str # (both ends removed)
#从字符串两端去除指定的字符集chars中的所有字符
#如果chars没有指定, remove whitespace characters from both ends
Lstrip ([chars]) str #从左开始
Rstrip ([chars]) str #从右开始
String Lookup * * * #返回索引号 #格式num. Find (' s ', 0,6) do not specify a number from the beginning
Find (sub[, start[, end]), int
#在指定的区间 [Start, end], from left to right, finds the substring sub. Returned index not found, returned-1
RFind (sub[, start[, end]), int
#在指定的区间 [Start, end], right to left, find the substring sub. Returned index not found, returned-1
Index (sub[, start[, end]), int
#在指定的区间 [Start, end], from left to right, finds the substring sub. The returned index was found, and the thrown exception was not found ValueError
Rindex (sub[, start[, end]), int
#在指定的区间 [Start, end], from left to right, finds the substring sub. The returned index was found, and the thrown exception was not found ValueError
Count (sub[, start[, end]), int
#在指定的区间 [Start, end], left to right, count the number of sub-string sub occurrences
Len (String) #返回字符串的长度, that is, the number of characters
Time complexity: The index and Count methods are both O (n), and the efficiency decreases as the list data size increases
String Judgment * * * #返回值为布尔型
#endswith (suffix[, start[, end]), BOOL
#在指定的区间 [Start, end], whether the string is suffix end
#startswith (prefix[, start[, end]), BOOL
#在指定的区间 [Start, end], whether the string is prefix start
String Judging is series
Isalnum ()-BOOL is a letter and number composition
Isalpha () Whether it is a letter
Isdecimal () contains only decimal digits
IsDigit () Whether all numbers (0~9)
Isidentifier () is not the beginning of letters and underscores, others are letters, numbers, underscores
Islower () are all lowercase
Isupper () All Caps
Isspace () contains only white space characters
String Formatting (c-style comparison of clutter need to remember more)
Formatting strings is a way to stitch the output style of a string, more flexible and convenient
#join拼接只能使用分隔符, and the overlapping objects are required to be spliced
#+ stitching strings is convenient, but non-strings need to be converted to * * string before stitching
Prior to version 2.5, only printf style-style print output can be used
#printf-style formatting, printf functions from the C language
Format requirements
#占位符: use% and format characters, such as%s,%d, and so on
#s调用str (), R calls Repr (). All objects can be converted by these two.
#占位符中还可以插入修饰字符, for example,%03d to print 3 positions, not enough for the front 0
#format% values, the format string and the formatted value are separated by%
#values只能是一个对象, or a tuple that has an equal number of placeholders for the format string, or a dictionary
String Formatting * * * (formatted with the format () function, Python recommended version)
*format function format string syntax--python encourage use *
# "{} {XXX}". Format (*args, **kwargs), str
#args是位置参数, is a tuple
#kwargs是关键字参数, is a dictionary
#花括号表示占位符
#{} means that the positional parameter is matched sequentially, and {n} represents a value that takes the positional parameter index to n
#{xxx} indicates that the keyword parameter searches for a consistent name
#{{}} indicates print curly braces
#位置参数
"{}:{}". Format (' 192.168.1.100 ', 8888), which is the placeholder for replacing the preceding format string with positional parameters in positional order
#关键字参数或命名参数
"{server} {1}:{0}". Format (8888, ' 192.168.1.100 ', server= ' Web server Info: '), positional parameters are matched by ordinal,
#关键字参数按照名词匹配
? #访问元素
"{0[0]}. {0[1]} ". Format ((' magedu ', ' com ')
#对象属性访问
From collections Import Namedtuple
Point = Namedtuple (' point ', ' X y ')
p = Point (4,5)
"{{{0.x},{0.y}}}". Format (P)
#对齐
' {0}*{1}={2:<2} '. Format (3,2,2*3) #冒号前面是索引号
' {0}*{1}={2:<02} '. Format (3,2,2*3) #可以写成 {: 0<3}.format (6), the position of Zilin can be replaced by other characters also supported
' {0}*{1}={2:>02} '. Format (3,2,2*3) #与上一个相反
' {: ^30} '. Format (' centered ') #代表中间对齐
' {:* ^30} '. Format (' centered ') #中间对齐0补空白位置
#进制
"int: {0:D}; Hex: {0:x}; Oct: {0:o}; Bin: {0:b} ". Format #0是索引号, D decimal, x 16 binary, o octal, b binary
"int: {0:D}; Hex: {0: #x}; Oct: {0: #o}; Bin: {0: #b} ". Format (#前面加成ox) ...
Convert the following number to output:
octets = [192, 168, 0, 1]
' {: 02x}{:02x}{:02x}{:02x} '. Format (*octets) #*octets, which is to convert the above list into format 16 binary
#浮点数
Print ("{}". Format (3**0.5)) # 1.7320508075688772
Print ("{: g}". Format (3**0.5)) # 1.73205
Print ("{: F}". Format (3**0.5)) # 1.732051
Print ("{: 10f}". Format (3**0.5)) # Right-aligned #
Print ("{: 2}". Format (102.231)) # 2 width
Print ("{:. 2}". Format (3**0.5)) # 1.7 2 digits
Print ("{:. 2f}". Format (3**0.5)) # 1.73 2 digits after the decimal point
Print ("{: 3.2f}". Format (3**0.5)) # 1.73 width 3, 2 digits after decimal point
Print ("{: 3.3f}". Format (0.2745)) # 0.275 #%3 control width
Print ("{: 3.3%}". Format (1/3)) # 33.333% #%.3 Control Accuracy
Python learning Essay (c) _ String