Part2_字符串___2023-02-23

目录


字符串

标识字符串

有三种方式:

  • 单引号:
    • a='She said"Yes!"'
    • 字符串的内容中允许有双引号
  • 双引号:
    • b="I'm fine."
    • 字符串的内容中允许有单引号
  • 三引号:
    • c="""
      There're some "words"
      in these two lines.
      """
      
    • 字符串的内容中允许有单/双引号, 也会保留多行内容

单独的三引号是文档注释

字符串的使用

非常灵活, 非常人性化 以下是一些例子

>>> "ha"*5+"!"*3
'hahahahaha!!!'

使用说明

列出模块中的函数

dir(m)可以以数组形式, 列出改模块中所有的函数名

>>> import math
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']

>>> dir(__builtins__)会展示所有 内置函数

带有双下划线的内容一般用于较为复杂的编程, 可以先忽略

函数说明

help(function)打印出改函数的使用说明

打印文档字符串

print(fucntion.__doc__)

>>> print(sin.__doc__)
Return the sine of x (measured in radians).