一、random模块

  random模块可以很容易生成随机数和随机字符串。

random.randint(1, 100)  # 1-100之间取一个随机数
random.randrange(1, 100) # 1-100之间取一个随机数

  randint&randrange区别:randint包含100,randrange不包含100!!!

random.random()   # 返回一个随机浮点数
random.choice('huangisngiisha12131a@!!!') # 返回一个给定数据集合中的随机字符
random.sample('adwas1231as!@#!as', 4) # 从多个字符中选取特定数量的字符

  示例如下:

>>> random.random()
0.8102972151157363
>>> random.choice('huangisngiisha12131a@!!!')
'a'
>>> random.sample('adwas1231as!@#!as',4)
['@', '!', '', 's']

二、生成随机字符串(string模块)

  string模块常用方法:

>>> import string

>>> string.digits  # 数字字符串
''
>>> string.ascii_letters # 字母字符串
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase # 小写字母字符串
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_uppercase # 大写字母字符串
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.punctuation # 特殊字符
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

  生成随机字符串:

>>> import string
>>> s = string.ascii_lowercase + string.digits
>>> s
'abcdefghijklmnopqrstuvwxyz0123456789'
>>> random.sample(s, 5)
['p', 'c', '3', 'l', 'y']
>>> ''.join(random.sample(s,5))
'1lngp'

三、random.shuffle()

  shuffle方法将序列的所有元素随机排序

>>> li = list(range(20))
>>> random.shuffle(li) # 打乱排序
>>> li
[5, 1, 4, 6, 18, 10, 9, 2, 8, 7, 16, 17, 15, 12, 14, 13, 0, 11, 19, 3]

random模块/string模块的更多相关文章

  1. day5-随机数相关:random模块&string模块

    一.概述 随机数在程序设计中的属于比较基础的内容,主要用于验证场景(如验证码,生成账号对应的密码等),今天结合random模块和string模块来谈谈python中随机数那些事儿. 二.随机数实现相关 ...

  2. random和string模块

    random模块import randomprint(random.random()) #随机打印一个浮点数print(random.randint(1,5)) #随机打印一个整数,包括5print( ...

  3. Python random模块&string模块 day3

    一.random模块的使用: Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. 1.常用函数: (1)random.random() 用于生成一个0到1 ...

  4. s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译

    时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...

  5. string模块

    string模块 string模块包括了一些字符串常量, 并且有str对象的功能,主要用来生成一些字符串.字符串格式化等 参考: http://python.usyiyi.cn/python_278/ ...

  6. Python 3之str类型、string模块学习笔记

    Windows 10家庭中文版,Python 3.6.4, Python 3.7官文: Text Sequence Type — str string — Common string operatio ...

  7. python标准库介绍——4 string模块详解

    ==string 模块== ``string`` 模块提供了一些用于处理字符串类型的函数, 如 [Example 1-51 #eg-1-51] 所示. ====Example 1-51. 使用 str ...

  8. Python之string模块(详细讲述string常见的所有方法)

    相信不少学习python的程序员都接触过string模块 string模块主要包含关于字符串的处理函数 多说无益,初学python的小伙伴还不赶紧码起来 接下来将会讲到字符串的大小写.判断函数. 以及 ...

  9. 小白的Python之路 day5 random模块和string模块详解

    random模块详解 一.概述 首先我们看到这个单词是随机的意思,他在python中的主要用于一些随机数,或者需要写一些随机数的代码,下面我们就来整理他的一些用法 二.常用方法 1. random.r ...

随机推荐

  1. Python数据结构,计算问题

    2018-08-12   <Python 算法>以及<用Python解决数据结构和算法> 什么是算法? 在计算机的世界中,算法本质上是我们对某一个问题或则某一类问题的解决方案. ...

  2. HTML中title前面小图标的实现

    做一个比较正规的网站,需要在标题栏前面加上相应的小图标,会好看的多.效果大致如下: 其实实现起来很简单,只要在html文件中的<head></head>标签中加上: <l ...

  3. GDI绘图写的简单扫雷

    由于没话多少时间,这个扫雷我只实现了主要功能(扫雷功能,递归实现) 废话不多说,直接上代码 using System; using System.Collections.Generic; using ...

  4. flask 的上下文管理

    Flask的上下文对象 Flask有两种Context(上下文),分别是 RequestContext 请求上下文 Request 请求的对象,封装了Http请求(environ)的内容 Sessio ...

  5. CF1012B Chemical table(构造)

    [Luogu-CF1012B] 还有重题 P5089[eJOI2018]元素周期表 题解原话 : 可以发现这个过程是不改变二分图中的连通分量的个数的 答案就是 连通分量数-1 证明 : 设一行或一列为 ...

  6. 从cocos2d-html5中提取出来的,用做前端开发的框架——cc.js

    从cocos2d-html5中提取出来的,用做前端开发的框架——cc.js /************************************************************* ...

  7. mutillidae之注册页面的Insert型报错注入

    http://127.0.0.1/mutillidae/index.php?page=register.php 1.注册一个用户试一试,发现页面只提示用户注册成功信息,并五其它可回显信息,果断尝试盲注 ...

  8. 1.nginx安装和配置

    1.安装 1.1安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel wget 1. ...

  9. git push的一些坑

    在安装git的时候我们一般会自己设置一个用户名和邮箱,这个一般设置为全局的用户名,如下所示 git config --global user.name "xxx" git conf ...

  10. pandas中,dataframe 进行数据合并-pd.concat()

    ``# 通过数据框列向(左右)合并 a = pd.DataFrame(X_train) b = pd.DataFrame(y_train) # 合并数据框(合并前需要将数据设置成DataFrame格式 ...