Characters and rules that variable names can contain
Lowercase letters (a~z)
Number (0~9)
Underscore (_)
The name is not allowed to start with a number
Python reserved keywords are not allowed
1.2 Numbers
Operator
description
Example
+
addition
5+8=13
-
Subtraction
90-10=80
*
multiplication
4*7=28
/
Floating point division
7/2=3.5
//
Integer division
7//2=3
%
Modulus (remainder)
7%3=1
**
power
3**4=81
1.2.1 Integer
Operational equivalence
# +、-、*、/、//、**
a =100
a = a -3 等价 a -=3
1.2.2 Cardinality
Base
0b or 0B stands for binary
0o or 0O stands for octal
0x or 0X stands for hexadecimal
1.3 String
Use of quotation marks
Double quotation marks and single quotation marks nested—>make the string contain quotation marks
Triple quotes—>input and output multi-line strings
Use \escaping
\nNew line
\t tab
\"Show quotation marks
\\Display single slash
Splicing operation
Use + stitching
Use *copy
Use [] to extract characters
>>>letter ='you are, you are'>>>letter[-1]'e'
Use [start: end :step] to slice
[:]
[start:]
[:end] Extract from the beginning to end-1
[start:end] From the beginning to end-1
[start: end :step]
The offset starts from 0, 1 from left to right, and increases in sequence
From right to left, start from -1, -2 and decrease in turn
Use len() to get the length
You can also use len() for other sequence types
Use split() to split string.function(argumens)
The built-in string function split() can split a string into a list of several substrings based on the separator
>>>a ='you are , you are beautiful.'>>>a.split(',')['you are','you are beautiful.']
Use join() to merge string.join(list)
The join() function decomposes a list containing several substrings, and combines these substrings into a complete large string
>>>str_list =['you are','you are beautiful.']>>>str_list2 =','.join(str_list)>>>print(str_list2)
you are, you are beautiful.
Commonly used string functions
a.startswith('all') —> Determine whether the string a starts with all
a.endswith('all') —> Determine whether a ends with all
a.find('all') —>Find the position (offset) where the word all first appears in a
a.rfind('all') —> the offset of the last occurrence of all
a.count('all') —> how many times all appears in a
a.isalnum() —> Determine whether all characters in a are letters or numbers
Use replace() to replace
replace() performs simple substring replacement (the substring that needs to be replaced, the new substring used for replacement, and how many places need to be replaced) if the last parameter is omitted, only the first occurrence will be replaced by default
>>>setup.replace('duck','marmoset')'a marmoset goes into a bar...'#修改最多100处>>>setup.repalce('a ','a famous',100)'a famous duck goes into a famous bar...'
Not commonly used string functions (case and alignment)
>>>setup ='a duck goes into a bar...'#将字符串收尾的.都删除>>>setup.strip('.')'a duck goes into a bar'#让字符串首字母变成大写>>>setup.capitalize()'A duck goes into a bar...'#让所有单词开头字母都变成大写>>>setup.title()'A Duck Goes Into A Bar...'#让所有字母都转换为大写>>>setup.upper()'A DUCK GOES INTO A BAR...'#将所有字母转换为小写>>>setup.lower()'a duck goes into a bar...'#将所有字母的大小写转换>>>setup.swapcase()'a DUCK GOES INTO A BAR...'#假设setup字符串被排版在指定长度(这里是30个字符)的空间里#在30个字符位居中>>>setup.center(30)' a duck goes into a bar... '#左对齐>>>setup.ljust(30)'a duck goes into a bar... '#右对齐>>>setup.rjust(30)' a duck goes into a bar...'