问题:在Python中,如何在一行代码中创建n个字符的字符串?

我需要在Python中生成一个包含n个字符的字符串。使用现有的Python库是否可以实现这一目标?例如,我需要一个由10个字母组成的字符串:

string_val = 'abcdefghij'

I need to generate a string with n characters in Python. Is there a one line answer to achieve this with the existing Python library? For instance, I need a string of 10 letters:

string_val = 'abcdefghij'

回答 0

要将同一字母重复10次:

string_val = "x" * 10  # gives you "xxxxxxxxxx"

而且,如果您想要更复杂的东西,例如n随机的小写字母,它仍然只是一行代码(不计算import语句并定义n):

from random import choice
from string import ascii_lowercase
n = 10

string_val = "".join(choice(ascii_lowercase) for i in range(n))

To simply repeat the same letter 10 times:

string_val = "x" * 10  # gives you "xxxxxxxxxx"

And if you want something more complex, like n random lowercase letters, it’s still only one line of code (not counting the import statements and defining n):

from random import choice
from string import ascii_lowercase
n = 10

string_val = "".join(choice(ascii_lowercase) for i in range(n))

回答 1

前十个小写字母为string.lowercase[:10](当然,如果您string先前已导入标准库模块,则为-)。

“使10个字符组成字符串”的其他方法:('x'*10所有10个字符均为小写xs;-),''.join(chr(ord('a')+i) for i in xrange(10))(又是前十个小写字母),等等,等等;-)。

The first ten lowercase letters are string.lowercase[:10] (if you have imported the standard library module string previously, of course;-).

Other ways to “make a string of 10 characters”: 'x'*10 (all the ten characters will be lowercase xs;-), ''.join(chr(ord('a')+i) for i in xrange(10)) (the first ten lowercase letters again), etc, etc;-).


回答 2

如果您只需要任何字母:

 'a'*10  # gives 'aaaaaaaaaa'

如果要连续字母(最多26个):

 ''.join(['%c' % x for x in range(97, 97+10)])  # gives 'abcdefghij'

if you just want any letters:

 'a'*10  # gives 'aaaaaaaaaa'

if you want consecutive letters (up to 26):

 ''.join(['%c' % x for x in range(97, 97+10)])  # gives 'abcdefghij'

回答 3

为什么要“一行”?您可以将任何东西放在一行上。

假设您希望它们以“ a”开头,并且每次增加一个字符(环绕> 26),则显示以下一行:

>>> mkstring = lambda(x): "".join(map(chr, (ord('a')+(y%26) for y in range(x))))
>>> mkstring(10)
'abcdefghij'
>>> mkstring(30)
'abcdefghijklmnopqrstuvwxyzabcd'

Why “one line”? You can fit anything onto one line.

Assuming you want them to start with ‘a’, and increment by one character each time (with wrapping > 26), here’s a line:

>>> mkstring = lambda(x): "".join(map(chr, (ord('a')+(y%26) for y in range(x))))
>>> mkstring(10)
'abcdefghij'
>>> mkstring(30)
'abcdefghijklmnopqrstuvwxyzabcd'

回答 4

这可能有点问题,但是对于那些对生成的字符串的随机性感兴趣的人,我的答案是:

import os
import string

def _pwd_gen(size=16):
    chars = string.letters
    chars_len = len(chars)
    return str().join(chars[int(ord(c) / 256. * chars_len)] for c in os.urandom(size))

请参阅这些 答案random.py的资料以获取更多见解。

This might be a little off the question, but for those interested in the randomness of the generated string, my answer would be:

import os
import string

def _pwd_gen(size=16):
    chars = string.letters
    chars_len = len(chars)
    return str().join(chars[int(ord(c) / 256. * chars_len)] for c in os.urandom(size))

See these answers and random.py‘s source for more insight.


回答 5

如果可以使用重复的字母,则可以使用*运算符:

>>> 'a'*5

'aaaaa'

If you can use repeated letters, you can use the * operator:

>>> 'a'*5

'aaaaa'

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。