Day Notes - string operation (escape conventional method, the output format), data type conversion, and a list of two methods of operation

First, the escape string 
\ n newline ------> newline
\ t vertical tab ------> Click ab key == "" (four spaces)
\\ backslash the symbol ------> \
R & lt ------> before the string plus r, for example, possible to prevent the string "\ r \ n \ b"
expand:
\ 'single quote \ "A \' -------> 'A'
\ "double quote \" A \ "-------->" A "
\ A bell python \ ajava --------> python? Java '
\ B backspace Python \ bphp --------> pythonphp
\ 0 spaces Python \ 0php ---------> Python PHP
\ R & lt carriage ------> enter key
Second, the conventional method of the string
1, join methods: string concatenation
example:
input:
. D = '*' the Join ( 'CONT')
Print (D)
Output: O * * C * T n-

2, Find method: Find String element specified
default is traversed to find a string may be specified search
3, count method: Find string specified element The number of
default statistics all, you can also specify Find
4, replace methods: replacement string in fragments
replace (a, b, c)
parameters a: string to be replaced string fragment
Parameter B: fragments replace the string
parameter c: the number of the replacement string segments the default is the replacement of all, this parameter can be used to control the number of times replacing
5, split method: split the string
split (a, b)
parameters of a: a string the string segment division point as
the parameter B: the default parameters as a whole is divided division point, can use this parameter to control the number of segmentation of
the final output is a list
. 6, upper method: all converted into lowercase letters capitalized string
7, lower method: the upper opposite
three, format formatted output: by the string as a template, the parameters passed to format, using {} placeholder
1, the primary usage
name = input ( "enter your name:")
info = "my name is: {}" format (name).
Print (info)
2, advanced usage:
2.1 by the underlying index value passed in {}, to control the data format of the position
s = "{0 name }, {2} age, gender: ". Format (" {1 } red "," F ",
Print (S)
2.2 by means of parameters passed in the {} are used to control the position of the data format of
s = "Name {name}, Age {} Age, Gender: {sex}". format ( name = " Small red ", sex =" female ", Age =" 18 is ")
Print (S)
when the advantage that, can be adjusted in the placement data format
2.3 by" {: a}. "format (b) to b accounting position designated length (a), (a> b, a space to fill the seats, the default fill the seats left)
left
name = "Sheng Yuan"
S = "my name is {: 5}, 25-year-old" .format (name)
Print (S)
right
name = "Sheng Yuan"
S = "my name is {:> 5}, 25 years old this year," .format (name)
Print (S)
middle
name = "Sheng Yuan"
S = "my name is {: 5} ^, 25 years old this year."the format (name)
Print (S)
2.4 format floating point control (number of decimal places)
Expression: s = "numbers: {:. af}" format ( b) a:. Decimal digits of the display, b: floating point format need
s = "numbers: {:. 2f}" format ( 2.222222).
print (s)
percentage value 2.5 formatting
expression: s = "numbers: {:. a%}". format (b) a: number of digits after the decimal point show the percentage, the default is 2, b: requires formatting the percentage of
s = "numbers: 0 {%} :." the format (.555555).
Print (S)
. 3, the conventional output format
# conventional methods formatted output
# to use placeholder%
#% F only placeholder numeric type, the display is in the form of decimal
#% d occupying only numeric type, the display is an integer of
#% s universal placeholder (strings, floating point, integer)
# SS = "Python :% s, Age:% I "% (" 6666 ", 29)
# SS =" Python:% s, Age:% D "% (" 6666 ", 29)
# Print (SS)
# Print (" name: % s, age:% d "% ( 'Zhonguo', 2019))
. 4, the extended expression F (preferably one, currently less popular)
# String Defines the time with the addition of F / F.
Name = "China"
Sex = "MALE"
Age = 18 is
S2 = F. "Name {name}, Age {age}, Sex Sex} {:"
Print (S2)
four , converting data types
# integer, float switch string, using STR
# string, floating-point to integer, using the int
# integer, floating-point string transfer, using a float
# integer, string, floating point, use BOOL
# NOTE: string to turn int / time folat, the contents of the string must be a number, (string not exist)
V. list
Definition: use [] to represent the list, the list element can be any type of
elements in the list use spaced
list elements operating
index value index
slice
len (strings, tuples, lists, dictionaries, set) Gets a list (number of elements) in the (length)

List1 = [ 'a',. 1, { ' name ':' Yuanxiao Song '}, True, 1.1]
# subscript values
S = List1 [2]
Print (S)
# slices, by the value of the element value of the index
List1 = S1 [-1: -3: -1]
Print (S1)
# len (), built-in function
S2 = len (List1)
Print (S2)
six, the operation method in the list of elements
increasing element method:
the append
INSERT
Extend
= Li [. 1, 2,. 3]
# the append method, is added to the end of the list elements
# li.append (999)
# Print (Li)
# iNSERT: Specifies the position of the insertion element
# iNSERT (index, Object)
# first parameter:
the second parameter #:
# li.insert (. 1, 25)
# Print (Li)
# Extend add multiple elements in a list
# li.extend ([11,222,2333])
# Print (Li)

to remove elements
remove : delete the specified elements of
pop: according to the index to delete the corresponding elements
clear: clear the list
# remove Note: remove elements that do not exist will complain
Li.remove # (2)
# Print (li)
# POP not pass parameters, default remove the last, to remove the target elements specified by the transfer parameter
# li.pop ()
# Print (li)
# the Clear
# li.clear ( )
# Print (li)

Guess you like

Origin www.cnblogs.com/yuanxiaosong/p/12110182.html