Python字符串|数字
在 Python3 中, string.digits
是一个预初始化的字符串,用作字符串常量。在Python中, string.digits
将给出小写字母“0123456789”。
Syntax : string.digits
Parameters : Doesn’t take any parameter, since it’s not a function.
Returns : Return all digit letters.
注意:确保导入字符串库函数以便使用string.digits
代码#1:
# import string library function
import string
# Storing the value in variable result
result = string.digits
# Printing the value
print(result)
输出 :
0123456789
代码#2:给定代码检查字符串输入是否只有数字字母
# importing string library function
import string
# Function checks if input string
# har only digits or not
def check(value):
for letter in value:
# If anything other than digit
# letter is present, then return
# False, else return True
if letter not in string.digits:
return False
return True
# Driver Code
input1 = "0123 456 789"
print(input1, "--> ", check(input1))
input2 = "12.0124"
print(input2, "--> ", check(input2))
input3 = "12345"
print(input3, "--> ", check(input3))
输出:
0123 456 789 --> False
12.0124 --> False
12345 --> True
应用:
字符串常量数字可以在许多实际应用中使用。让我们看一段代码,解释如何使用数字生成给定大小的强随机密码。
# 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_uppercase +
string.ascii_lowercase +
string.digits)
for n in range(size)])
return generate_pass
# Driver Code
password = rand_pass(10)
print(password)
输出:
2R8gaoDKqn