当前位置: 移动技术网 > IT编程>脚本编程>Python > Python运算函数+随机数函数+字符串操作

Python运算函数+随机数函数+字符串操作

2020年07月20日  | 移动技术网IT编程  | 我要评论
Python标准的数据类型NumbersStringListTupleDictionary基本运算函数序号函数说明1abs(x)绝对值2ceil(x)不小于x的最小整数3cmp(x, y)x≤y时为-1;x>y时为14fabs(x)绝对值(需从math模块导出)5floor(x)不大于x的最大整数6modf(x)返回x的整数和小数部分7round(x [,n])四舍五入;保留n位小数 或有效数字随机

Python标准的数据类型

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

基本运算函数

序号 函数 说明
1 abs(x) 绝对值
2 ceil(x) 不小于x的最小整数
3 fabs(x) 绝对值(需从math模块导出)
4 floor(x) 不大于x的最大整数
5 modf(x) 返回x的整数和小数部分
6 round(x [,n]) 四舍五入;保留n位小数 或有效数字

随机数函数

需要说明的是:以下函数不能直接访问,所以我们需要导入random随机模块,然后调用以下函数。
序号 函数 说明 操作
1 choice(seq) 返回seq的任意一项(seq可以为列表,元组或字符串) choice('A String') : n
2 randrange ([start,] stop [,step]) 返回范围内满足就间隔的随即项 randrange(100, 1000, 3) : 520
3 random() 返回0~1之间的随机数 random() : 0.281954791393
4 seed([x]) 设置用于生成随机数的整数起始值(无返回值) x一个随机数种子, 如果省略,那么它使用系统时间来生成随机数。
5 shuffle(lst) 随机化列表中的项目(无返回项) shuffle([20, 16, 10, 5]) : [16, 5, 20, 10]
6 uniform(x, y) 返回x~y之间的随机数 uniform(5, 10) : 5.52615217015

字符串操作

>> print('C:\\nowhere')
C:\nowhere
>> print(r'C:\\nowhere' # r'expression')
C:\\nowhere
>> print(u'Hello, world!') # Unicode允许使用更多样化的字符集,包括来自世界上大多数语言的特殊字符
Hello, world!
字符串内置函数
capitalize():将字符串的首字母大写

>> str = "this is string example....wow!!!";
>> print(str.capitalize())
This is string example....wow!!!
center(width, fillchar):以原字符串为中心,首尾添加fillchar使其长度达到width
>> str = "this is string example....wow!!!";
>> print(str.center(40, 'a'))
aaaathis is string example....wow!!!aaaa
count(str, beg= 0,end=len(string)):str在原字符串或子字符串中出现的次数
>> str = "this is string example....wow!!!";
>> print(str.count("i", 4, 40))
2
>> print(str.count("wow"))
1
endswith(suffix, beg=0, end=len(string)):原字符串或子字符串是否以suffix结尾
>> str = "this is string example....wow!!!";
>> print(str.endswith("wow!!!"))
True
>> print(str.endswith("wow!!!",20))
True
>> print(str.endswith("is", 2, 4))
True
>> print(str.endswith("is", 2, 6))
False
startswith(str, beg=0,end=len(string)):判断原字符串或子字符串是否以str开头

>> str = "this is string example....wow!!!";
>> print(str.startswith('this'))
True
>> print(str.startswith('is'),2,4)
True
>> print(str.startswith('this'),2,4)
False
find(str, beg=0 end=len(string)):在原字符串或子字符串中寻找给定字符串str,如果找到返回索引,否则返回-1
rfind(str, beg=0,end=len(string)):在原字符串或子字符串中寻找给定字符串str,如果找到返回最后一次找到的索引,否则-1
>> str = "this is string example....wow!!!";
>> print(str.find("is"))
2
>> print(str.rfind("is"))
5
>> print(str.rindex("is", 0, 10))
5
>> print(str.rindex("is", 10, 0))
-1
index(str, beg=0, end=len(string)):在原字符串或子字符串中寻找给定字符串str,如果找到返回第一次找到的索引,否则发生异常
rindex(str, beg=0 end=len(string)):在原字符串或子字符串中寻找给定字符串str,如果找到返回最后一次找到的索引,否则发生异常
>> str = "this is string example....wow!!!";
>> print(str.index("is"))
2
>> print(str.rindex("is"))
5
>> print(str.find("exam", 40))
ValueError: substring not found
isalnum():如果字符串由字符或数字组成,返回True;否则False
isalpha():如果字符串由字符组成,返回True;否则False
isdigit():如果字符串由数字组成,返回True;否则False
>> str = "this2009";  # No space in this string
>> print(str.isalnum())
True
>> str = "this is string example....wow!!!";
>> print(str.isalnum())
False
>> str = "this";  # No space in this string
>> print(str.isalpha())
True
>> str = "2009";  # No space in this string
>> print(str.isdigit())
True
islower():检测字符串是否都是小写
isupper()():检测字符串是否都是大写
isnumeric():检测unicode字符串是否都是数值
isdecimal():检测unicode字符串是否都是数值
isspace():检测字符串是否仅包含空格
lower():将所有字母转化为小写
upper()():将所有字母转化为大写
title():将所有字母转化为首字母大写
swapcase():颠倒所有原字母的大小写
istitle():检查字符串是否都是首字母大写
>> str = "This Is String Example...Wow!!!";
>> print(str.istitle())
True
>> str = "This is string example....wow!!!";
>> print(str.istitle())
False
str.join(sequence):用str将字符串sequence连接起来
>> s = "-";
>> seq = ("a", "b", "c"); # This is sequence of strings.
>> print(s.join(seq))
a-b-c
ljust(width[, fillchar]):保持字符串left对其,用fillchar对字符串进行填充,使其长度达到width
rjust(width,[, fillchar]):保持字符串right对其,用fillchar对字符串进行填充,使其长度达到width
>> str = "this is string example....wow!!!";
>> print(str.ljust(50, '0'))
this is string example....wow!!!000000000000000000
>> print(str.rjust(50, '0'))
000000000000000000this is string example....wow!!!
lstrip([chars]):去除字符串left的所有chars字符
rstrip([chars]):去除字符串right的所有chars字符
strip([chars]):同时执行lstrip和rstrip
>> str = "     this is string example....wow!!!     ";
>> str1 = "88888888this is string example....wow!!!8888888";
>> print(str.lstrip())
this is string example....wow!!!
>> print(str1 .lstrip('8'))     
this is string example....wow!!!8888888
>> print(str.rstrip())
     this is string example....wow!!!
>> print(str1 .rstrip('8'))
88888888this is string example....wow!!!
>> print(str1 .strip('8'))
this is string example....wow!!!
replace(old, new [, max]):将字符串中的old子字符串替换成new子字符串。如果给定参数max,则前述的操作只替换max次
str = "this is string example....wow!!! this is really string"

>> print(str.replace("is", "was", 3))
thwas was string example....wow!!! thwas is really string
split(str="", num=string.count(str)):按照str对字符串进行分割num次,如果不提供str和num,则默认按照空格进行全部分割

>> str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
>> print(str.split())
['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
>> print(str.split(' ', 1 ))
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
splitlines( num=string.count(’\n’)):按照换行符对字符串进行分割,如果num为真,则分割内容包括换行符,否则不包含
>> str = str = "Line1-a b c d e f\nLine2- a b c\n\nLine4- a b c d";
>> print(str.splitlines(True))
['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']
>> print(str.splitlines(2))
['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']
zfill (width):字符串左填0达到width长度
>> str = "this is string example....wow!!!";
>> print(str.zfill(40))
00000000this is string example....wow!!!

本文地址:https://blog.csdn.net/Haiyang_Duan/article/details/107447132

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网