Python数学函数和字符串函数

1、绝对值abs(x)

求数值的绝对值

>>> abs(-3.4)
3.4
>>>
2、最小值 min(x)

求最小值

>>> min(10,23,56,4,656,66)
4
>>>
3、最大值max(x)

求最大值

>>> max(10,23,56,4,656,66)
656
>>>
4、算术平方根sqrt(x)

使用前先加载math

>>> import math
>>> math.sqrt(3)
1.7320508075688772
>>>
5、取整ceil(x)

数值向上取整

>>> import math
>>> math.ceil(5.6)
6
>>>
6、向下取整floor(x)

数值向下取整

>>> import math
>>> math.floor(-5.6)
-6
>>> math.floor(5.6)
5
>>>
7、求字符串长度len(x)
>>> len('sdfsdfasdf')
10
>>>

8、将字符串转换成大写str.upper()
>>> str.upper('i love chaina!')
'I LOVE CHAINA!'
>>>
9、将字符串转换成小写str.lower()
>>> str.lower('I LOVE CHAINA!')
'i love chaina!'
>>>
10、字符串中查找指定字符串c str.find(c)

在字符串中查找指定的字符串,如果有返回索引值,没有返回-1

>>> str = 'i love chaina!'
>>> str.find('love')
2
>>> str.find('loves')
-1
>>>
11、字符串替换 str.replace(old,new)

将字符串中old字符串替换成new字符串

>>> str.replace('love','leves')
'i leves chaina!'
>>>
12、去空格 str.strip()

去除字符串str两侧的空格、回车

>>> str = '  i love chaina!    \n'
>>> print(str)
  i love chaina!

>>> str.strip()
'i love chaina!'
>>>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值