当前位置:   article > 正文

Python常用函数、方法示例总结(API)_python api函数

python api函数

本文重点放在常用函数、方法的总结,主要起“字典”的作用,忘了有啥方法可以查看此文速查,因此内容上做了些减法。结合Python语法基础来看可能效果会好一些。

1. 运算相关

x,y,z为某个数字;list为列表

  1. abs(x)
  2. divmod(x, y)
  3. pow(x, y)
  4. pow(x, y, z)
  5. round(x)
  6. round(x, d)
  7. min(list)
  8. max(list)
  9. sum(list)
  10. int(x)
  11. float(x)
  12. complex(x)

2. Sring与数字

string为某个字符串;num为某个数字;firstNum为第一个数字;lastNum为最后一个数字;step为步长;u为unicode编码字符;old为老的子串;new为新的子串;sep为分隔的子串规则;fillchar为填充的字符;chars为字符;

字符串的操作方法:

  1. string.title()
  2. string.lower()
  3. string.upper()
  4. string.replace(old, new)
  5. string.center(num)
  6. string.center(num, fillchar)
  7. string.rstrip()
  8. string.lstrip()
  9. string.strip()
  10. string.strip(chars)
  11. new.join(string)

字符串的获取功能:

  1. string.count('xxx')
  2. string.split()
  3. string.split(sep)
  4. len(string)
  5. chr(u)
  6. ord(x)

其他类型转字符串的方法:

  1. str(num)
  2. hex(num)
  3. oct(num)

字符串切片(同列表切片):

  1. string[firstNum: lastNum: step]
  2. string[::-1]

字符串的格式化:

    1. "{} {}".format("hello", "world") :不设置指定位置,按默认顺序输出 hello world;
    2. "{1} {0} {1}".format("hello", "world") :设置指定位置,输出 world hello world;
    3. format函数传入对象:
    4. class AssignValue(object):
    5. def __init__(self, value):
    6. self.value = value
    7. my_value = AssignValue(6)
    8. print('value 为: {0.value}'.format(my_value)) # "0" 是可选的
     

数字的格式化:

    1. {字符串模板}.format(用逗号分隔的字符串参数) :使用字符串模板格式化字符串参数;
    2. format格式化函数的字符串模板,如下图所示
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/791979
推荐阅读
相关标签
  

闽ICP备14008679号