在Python3中, ascii_字母 是用作字符串常量的预初始化字符串。 ascii_字母 基本上是 ascii_小写 和 ascii_大写字母 字符串常量。此外,生成的值不依赖于语言环境,因此不会更改。
null
语法:
string.ascii_letters
注: 确保导入字符串库函数以便使用 ascii_字母 .
参数:
Doesn't take any parameter, since it's not a function.
返回:
Return all ASCII letters (both lower and upper case)
代码#1:
# import string library function import string # Storing the value in variable result result = string.ascii_letters # Printing the value print (result) |
输出:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
代码#2: 给定的代码检查字符串输入是否只有ASCII字符。
# importing string library function import string # Function checks if input string # has only ascii letters or not def check(value): for letter in value: # If anything other than ascii # letter is present, then return # False, else return True if letter not in string.ascii_letters: return False return True # Driver Code input1 = "GeeksForGeeks" print (input1, "--> " , check(input1)) input2 = "Geeks for Geeks" print (input2, "--> " , check(input2)) input3 = "Geeks_for_geeks" print (input3, "--> " , check(input3)) |
输出:
GeeksForGeeks --> True Geeks for Geeks --> False Geeks_for_geeks --> False
应用: 字符串常量ascii_字母可用于许多实际应用。 我们来看一段代码,解释如何使用ascii_字母生成给定大小的强随机密码。
代码#1:
# Importing random to generate # random string sequence import random # Importing string library function import string def rand_pass(size): # Takes random choices from # ascii_letters and digits generate_pass = ''.join([random.choice( string.ascii_letters + string.digits) for n in range (size)]) return generate_pass # Driver Code password = rand_pass( 10 ) print (password) |
输出:
oQjI5MOXQ3
注: 以上给定的代码将根据提供的大小,每次打印随机(不同)密码。 代码#2: 假设您想生成随机密码,但要从给定字符串集生成。让我们看看如何使用ascii_字母实现这一点:
# Importing random to generate # random string sequence import random # Importing string library function import string def rand_pass(size, scope = string.ascii_letters + string.digits): # Takes random choices from ascii_letters and digits generate_pass = ''.join([random.choice(scope) for n in range (size)]) return generate_pass # Driver Code password = rand_pass( 10 , 'Geeks3F0rgeeKs' ) print (password) |
输出:
kg3g03keG3
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END