Python中的字符串.octdigits
在 Python3 中, string.octdigits
是一个预初始化的字符串,用作字符串常量。在Python中, string.octdigits
将给出八进制字母“01234567”。
Syntax : string.octdigits
Parameters : Doesn’t take any parameter, since it’s not a function.
Returns : Return all octadecimal digit letters.
注意:确保导入字符串库函数以便使用string.octdigits
代码#1:
# import string library function
import string
# Storing the value in variable result
result = string.octdigits
# Printing the value
print(result)
输出 :
01234567
代码 #2:给定代码检查字符串输入是否只有八进制数字字母
# importing string library function
import string
# Function checks if input string
# has only octdigits or not
def check(value):
for letter in value:
# If anything other than octdigit
# letter is present, then return
# False, else return True
if letter not in string.octdigits:
return False
return True
# Driver Code
input1 = "01234567"
print(input1, "--> ", check(input1))
input2 = "abcdefABCDEF"
print(input2, "--> ", check(input2))
input3 = "abcdefghGEEK"
print(input3, "--> ", check(input3))
input4 = "0123"
print(input3, "--> ", check(input4))
input5 = "567"
print(input3, "--> ", check(input5))
输出:
01234567 --> True
abcdefABCDEF --> False
abcdefghGEEK --> False
abcdefghGEEK --> True
abcdefghGEEK --> True
应用:
字符串常量octdigits可以在许多实际应用中使用。让我们看一段代码,解释如何使用数字生成给定大小的强随机密码。
# Importing random to generate
# random string sequence
import random
# Importing string library function
import string
def rand_pass(size):
# Takes random choices from
# string.octdigits
generate_pass = ''.join([random.choice(string.octdigits)
for n in range(size)])
return generate_pass
# Driver Code
password = rand_pass(10)
print(password)
输出:
5077306643