变量: 用于存放程序运行过程中要使用的中间值.
单词之间使用单词的首字母进行大写用于区分.
单词与单词之间使用下划线分隔开
例如: get_info, num...等
pep8推荐的变量命名规范
变量名 = 整数
变量名 = 浮点数
变量名 = 字符串
可以将python变量理解为一张标签贴纸。 不论哪个类型都可以将这个变量名贴到数据类型上. 就如同C语言的void指针.
在之前,介绍的python对象存在基本的三要术: 标识(id)
, 类型(type)
, 值(value)
.
python变量存放的值, 实际上就是对象标识(id). 在CPython解释器中, id就是对象的内存地址.
‘...‘
)或双引号("..."
)或三引号("""..."""
或‘‘‘...‘‘‘
)引起来的字符称为字符串>>> "hello"
‘hello‘
>>> "你好"
‘你好‘
>>> ‘world‘
‘world‘
>>> """python
... hello"""
‘python\nhello‘
字符串是不可变对象. 涉及到字符串字面值修改的操作均是返回一个新的字符串对象
+
): 用于连接两个字符串*
): 用于重复n
次字符串>>> "hello "
‘hello ‘
>>> _ + "world" # _内置变量, 用于保存最近一次输出的值. 不建议对其进行赋值
‘hello world‘
>>> "py" "thon" # 自由字面值才可以, 变量和字面值是不可以自动拼接, 必须使用 + 进行拼接
‘python‘
>>> "hel" * 3
‘helhelhel‘
>>> "hel " * 3
‘hel hel hel ‘
>>> py = "py"
>>> th = "thon"
>>> py + th
‘python‘
>>>
>>> "python"[1]
‘y‘
>>> "python"[-1]
‘n‘
>>> "python"[0]
‘p‘
>>> "python"[6] # 超出索引就会报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> "python"[5] # 索引最长为 字符串长度-1
‘n‘
step-1
个位置取一个值到子字符串>>> "python"[0:3]
‘pyt‘
>>> "python"[:3]
‘pyt‘
>>> "python"[2:5]
‘tho‘
>>> "python"[3:]
‘hon‘
>>> "python"[1:5:2]
‘yh‘
变量0, 变量1, 变量2, ..., 变量n-1 = 长度为n的字符串
: 将每个字符赋值到对应位置变量变量0, 变量1,...,变量i-1 *变量 = 长度为n的字符串
: 将字符串的前i个字符分别赋值到对应位置变量. 其余字符打包成列表赋值給*
后的变量>>> p,y,t,h,o,n = "python"
>>> p
‘p‘
>>> y
‘y‘
>>> t
‘t‘
>>> h
‘h‘
>>> o
‘o‘
>>> n
‘n‘
>>> p, y, *th = "python"
>>> p
‘p‘
>>> y
‘y‘
>>> th
[‘t‘, ‘h‘, ‘o‘, ‘n‘]
>>> len("hello world")
11
>>> "hello world".capitalize()
‘Hello world‘
>>> "Hello".casefold()
‘hello‘
>>> "hello".center(20)
‘ hello ‘
>>> "hello".center(20, "*")
‘*******hello********‘
>>> "hello".count("l")
2
>>> "hello".count("ll")
1
>>> "hello".count("ll", -1, -4)
0
>>> "hello".count("ll", 2, 4)
1
>>> "hello".encode()
b‘hello‘
>>> "hello".endswith("o")
True
>>> "hello".endswith("lo")
True
>>> "hello".endswith("l")
False
>>> "hello".endswith("l", -2, -5) # 指定判断区间
False
>>> "hello".endswith("l", -5, -2)
True
>>> "hello".find("l") # 存在返回子字符串的索引
2
>>> "hello".find("w") # 不存在返回 -1
-1
>>> "hello".find("ll")
2
>>> "hello".find("ll", 1,4)
2
>>> "hello".rfind("l")
3
>>> "hello".index("l")
2
>>> "hello".index("ll")
2
>>> "hello".index("w") # 不存在会报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> "hello".rindex("l")
3
>>> ",".join("hello")
‘h,e,l,l,o‘
>>> ",".join(["h", "e", "l","l" ,"o"])
‘h,e,l,l,o‘
>>> "hello".ljust(10) # 左对齐
‘hello ‘
>>> "hello".ljust(10, "*")
‘hello*****‘
>>> "hello".rjust(10) # 右对齐
‘ hello‘
>>> "hello".rjust(10, "*")
‘*****hello‘
>>> "hello".upper()
‘HELLO‘
>>> "HELLO".lower()
‘hello‘
>>> " hello ".strip() # chars默认是去除空格
‘hello‘
>>> " .com hello .com ".strip(" .com") # 迭代从chars中
‘hell‘
>>> " hello ".lstrip()
‘hello ‘
>>> " hello ".rstrip()
‘ hello‘
>>> " .com hello .com ".lstrip(" .com")
‘hello .com ‘
>>> " .com hello .com ".rstrip(" .com")
‘ .com hell‘
>>> "hello".partition("l")
(‘he‘, ‘l‘, ‘lo‘)
>>> "hello".partition("e")
(‘h‘, ‘e‘, ‘llo‘)
>>> "hello".partition("w") # sep不在字符串中
(‘hello‘, ‘‘, ‘‘)
>>> "hello".rpartition("l")
(‘hel‘, ‘l‘, ‘o‘)
>>> "hello".replace("l", "r") # 默认count完全替换
‘herro‘
>>> "hello".replace("l", "r", 1) # 指定替换次数, 从左到右
‘herlo‘
>>> "hello".split("l", 1)
[‘he‘, ‘lo‘]
>>> "hello".split("l")
[‘he‘, ‘‘, ‘o‘]
>>> "hello".rsplit("l", 1)
[‘hel‘, ‘o‘]
>>> "love python".title()
‘Love Python‘
format_string % values
"%填充符"
"%[(name)][flags][width].[precision]typecode"
In [1]: "%s" % "hello world" # %s: 填充字符串时使用, 在填充可以转为字符串的内容时, 也可以使用
Out[1]: ‘hello world‘
In [2]: "%(name)s" % {"name": "dyp"} # 通过字典映射值.
Out[2]: ‘dyp‘
In [6]: "%f" % 3.14 # %f: 填充浮点数, 默认保留6位小数, 不足用0补齐
Out[6]: ‘3.140000‘
In [7]: "%d" % 10 # %d: 填充整数
Out[7]: ‘10‘
In [8]: "%c" % "h" # %c: 填充一个字符. ASCII字符才能被填充进去
Out[8]: ‘h‘
In [18]: "%e" % 300.31 # %e: 指数形式浮点数
Out[18]: ‘3.003100e+02‘
In [19]: "%o" % 10 # 有符号八进制整数
Out[19]: ‘12‘
In [20]: "%x" % 10 # 有符号十六进制整数
Out[20]: ‘a‘
In [21]: "%#x" % 10 # 井号(#), 八进制, 填充0o, 十六进制填充0x
Out[21]: ‘0xa‘
In [22]: "%#o" % 10
Out[22]: ‘0o12‘
In [23]: "%5d" % 10 # 5, 指定格式化的宽度. 及总共格式5个字符
Out[23]: ‘ 10‘
In [24]: "%05d" % 10 # 5前面的0, 使用0填充整个格式字符串到5. 只有右对齐时, 0填充符才会生效
Out[24]: ‘00010‘
In [25]: "%-5d" % 10 # -, 表示左对齐
Out[25]: ‘10 ‘
In [28]: "%+5d" % 10 # +, 显示数的正负号.
Out[28]: ‘ +10‘
In [29]: "%(name)s: %(age)d" % {"name": "dyp", "age": 22} # 关键字映射格式
Out[29]: ‘dyp: 22‘
使用{}
在字符中进行占位. 在使用format()
方法对占位进行填充
"{[index|arguments]:[fill][alignment][width].[precision][typecod]}"
<
>
^
(可选, 默认右对齐)<
: 左对齐>
: 右对齐^
: 居中对齐In [37]: "{}‘s age is {}".format("Array", 10) # 正常按位置填充
Out[37]: "Array‘s age is 10"
In [38]: "{2}‘s age is {1}".format("Array", 10, "Marry") # 指定位置填充
Out[38]: "Marry‘s age is 10"
In [39]: "{name}‘s age is {age}".format("Mary", name="Array", age=10) # 关键值填充
Out[39]: "Array‘s age is 10"
In [40]: "{name}‘s age is {age:f}".format("Mary", name="Array", age=10) # 可以指定格式化数据的类型
Out[40]: "Array‘s age is 10.000000"
In [41]: "{name}‘s age is {age:d}".format("Mary", name="Array", age=10)
Out[41]: "Array‘s age is 10"
In [42]: "{:#b}".format(10) # 对进制进行格式. 二进制
Out[42]: ‘0b1010‘
In [43]: "{:b}".format(10)
Out[43]: ‘1010‘
In [44]: "{:o}".format(10) # 八进制
Out[44]: ‘12‘
In [45]: "{:#o}".format(10)
Out[45]: ‘0o12‘
In [46]: "{:x}".format(10) # 十六进制
Out[46]: ‘a‘
In [47]: "{:#x}".format(10)
Out[47]: ‘0xa‘
In [57]: "{:5d}".format(10) # 宽度指定
Out[57]: ‘ 10‘
In [58]: "{:05d}".format(10) # 填充符指定
Out[58]: ‘00010‘
In [59]: "{:0<5d}".format(10) # 对齐方式指定. 默认右对齐
Out[59]: ‘10000‘
In [60]: "{:5f}".format(3.14)
Out[60]: ‘3.140000‘
In [61]: "{:5.2f}".format(3.14)
Out[61]: ‘ 3.14‘
In [62]: "{:05.2f}".format(3.14)
Out[62]: ‘03.14‘
In [63]: "{:0<5.2f}".format(3.14)
Out[63]: ‘3.140‘
python3.6增加的功能. 类似与format_strinf.format()
f"{variable:[fill][alignment][width].[precision][typecod]}"
<
>
^
(可选, 默认右对齐)<
: 左对齐>
: 右对齐^
: 居中对齐In [68]: string = "hello world"
In [69]: f"{string}" # 正常使用
Out[69]: ‘hello world‘
In [71]: f"{string:20}" # 20: 指定宽度
Out[71]: ‘hello world ‘
In [72]: f"{string:>20}" # >: 指定对齐方式. 默认左对齐
Out[72]: ‘ hello world‘
In [73]: f"{string:0>20}" # 0: 指定的填充符
Out[73]: ‘000000000hello world‘
In [77]: f"{string:0^20s}" # s: 指定的格式对象的类型.
Out[77]: ‘0000hello world00000‘
In [78]: f"{10:b}" # 数进制格式, 二进制
Out[78]: ‘1010‘
In [79]: f"{10:#b}"
Out[79]: ‘0b1010‘
In [80]: f"{10:o}" # 八进制
Out[80]: ‘12‘
In [81]: f"{10:#o}"
Out[81]: ‘0o12‘
In [82]: f"{10:x}" # 十六进制
Out[82]: ‘a‘
In [83]: f"{10:#x}"
Out[83]: ‘0xa‘
原文:https://www.cnblogs.com/duyupeng/p/13045257.html