一个以字母开头的ascii_letters + digits的随机字符串

huangapple go评论64阅读模式
英文:

A random string of ascii_letters + digits, but only starts with a letter

问题

我使用以下简单代码生成长度为10的随机字符串:

from string import ascii_letters, digits
''.join(choice(ascii_letters + digits) for i in range(10))

问题是有时字符串的第一个字符是数字。我不想要那样。我希望第一个字符始终是字母,后面的字符我不在乎。

我可以通过连接两个字符串(一个长度为1,另一个长度为9),并根据ascii_letters生成第一个字符串来解决这个问题。不过,我想知道是否有更简单的方法。

英文:

I'm using the following simple code to generate a random string of length 10

from string import ascii_letters, digits
''.join(choice(ascii_letters + digits) for i in range(10))

The problem is that sometimes the first character of the string is a digit. I don't want that. I want the first character to be always a letter, and what comes after I don't care about.

I can solve this problem to joining two strings (one of length 1 and the other of length 9), and generating the first one based on the ascii_letters alone. However, I was wondering if there's a simpler approach.

答案1

得分: 2

import string
import random

def random_string_generator(length_str=10, asc_value=string.ascii_uppercase, chars=string.ascii_uppercase + string.digits):
both_str = ''.join(random.choice(chars) for _ in range(length_str-1))
print(f'{random.choice(asc_value)}{both_str}')
return f'{random.choice(asc_value)}{both_str}'

random_string_generator()

英文:
import string
import random

def random_string_generator(length_str=10, asc_value=string.ascii_uppercase, chars=string.ascii_uppercase + string.digits):
    both_str = ''.join(random.choice(chars) for _ in range(length_str-1))
    print(f'{random.choice(asc_value)}{both_str}')
    return f'{random.choice(asc_value)}{both_str}'

random_string_generator()

答案2

得分: 2

你还可以使用 random.choices() 来选择 k 个元素。不需要使用 rangefor

英文:

You can also use random.choices():

result = random.choice(ascii_letters) + ''.join(random.choices(ascii_letters + digits, k=9))

to select k elements. No need for range or for.

答案3

得分: 1

这应该适用于您的解决方案。对于索引 0,它应该始终从字母中进行选择。

from string import ascii_letters, digits
import random
''.join(random.choice(ascii_letters) if i == 0 else random.choice(ascii_letters + digits) for i in range(10))
英文:

I hope this works for your solution. For index 0 it should always choose from alphabets

from string import ascii_letters, digits
import random
''.join(random.choice(ascii_letters) if i == 0 else random.choice(ascii_letters + digits) for i in range(10))

答案4

得分: 0

以下是已翻译的内容:

如果您正在寻找在安全上下文中可用的随机性,您可能至少要考虑使用secrets模块来进行选择。根据用例,使用不适当上下文的答案可能与预期结果大幅 不同

以下内容适用于非安全关键上下文。

from string import ascii_letters, digits
from secrets import choice

result = choice(ascii_letters) + ''.join(choice(ascii_letters + digits) for _ in range(9))
英文:

If you are looking for randomness which is usable in security contexts, you may want to consider, at a minimum, using the secrets module for making choices. Depending on the use-case, the result of using an answer given for an inappropriate context could vastly differ from expected outcome.

The below can get the job done for non security-critical contexts.

from string import ascii_letters, digits
from secrets import choice

result = choice(ascii_letters) + ''.join(choice(ascii_letters + digits) for _ in range(9))

huangapple
  • 本文由 发表于 2023年2月8日 14:26:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定