Python 在每20个字符后添加换行符,并将结果另存为新字符串

Python 在每20个字符后添加换行符,并将结果另存为新字符串,python,Python,我有一个字符串变量input=“来自用户输入的非常长的字符串”,我如何循环字符串并在20个字符后添加换行符\n,然后将格式化的字符串保存在变量新输入中 到目前为止,我只能获得前20个字符,例如输入[0:20],但是如何在整个字符串中执行此操作并在该点添加换行符?您可能希望执行类似操作 inp=“来自用户输入的非常长的字符串” 新建_输入=“” 对于i,枚举中的字母(inp): 如果i%20==0: 新输入+='\n' 新输入+=字母 #这只是因为在开始时也添加了一个“\n”字符 新输入=新输入[

我有一个字符串变量
input=“来自用户输入的非常长的字符串”
,我如何循环字符串并在20个字符后添加换行符
\n
,然后将格式化的字符串保存在变量
新输入中


到目前为止,我只能获得前20个字符,例如
输入[0:20]
,但是如何在整个字符串中执行此操作并在该点添加换行符?

您可能希望执行类似操作

inp=“来自用户输入的非常长的字符串”
新建_输入=“”
对于i,枚举中的字母(inp):
如果i%20==0:
新输入+='\n'
新输入+=字母
#这只是因为在开始时也添加了一个“\n”字符
新输入=新输入[1:]

您可以访问正确长度的片段,而不是逐个遍历每个字符

text=“来自用户输入的非常长的字符串”
直线长度=5
行=[]
对于范围内的i(0,长度(文本),行长度):
行。追加(文本[i:i+行长度]+'\n')
打印(“”.连接(行))
您可以用列表替换
for
循环:

text=“来自用户输入的非常长的字符串”
直线长度=5
行=[text[i:i+行长度]+'\n'表示范围内的i(0,len(text),行长度)]
打印(“”.连接(行))
印刷品:

A ver
y ver
y lon
g str
ing f
rom u
ser i
nput
line_length
更改为
20
,以获得更长的实际输入


注意:如果问题的最后一行少于20个字符,则逐字阅读将省略最后一行换行符(
'\n'
)。如果愿意,您可以这样做,但如果要将其打印到屏幕或保存到文件中,您可能希望最后一行有一个换行符,即使它是较短的余数。

假设您需要为每N个字符拆分一个字符串

那你就可以用它了。 这将获得字符串列表,您只需将其与。 像这样:

some_str: str = "A very very long string from user input"
n: int = 20
splitted_str: List[str] = [some_str[i:i+n] for i in range(0, len(some_str), n)]
result: str = "\n".join(splitted_str)

除了
列表理解
+
连接
方法(顺便说一句,这非常方便),您还可以在以下方面使用python bulit:

要使其适用于每个
n个
字符,请执行以下操作:

n = 20
'\n'.join(re.findall('.{1,%i}' % n, string))

正如@CrazyChuck正确地假设的那样,这可能比列表理解方法需要更长的时间。例如,给定一个10亿字符的字符串(例如,
string='a'*10**9
),使用
列表理解方法需要10.6秒,使用
正则表达式方法需要12.6秒。更大的字符串可能会有很大的区别,但没有这么大的字符串也不会有问题。

这里有一个基本的、详细的方法,可以为每20个字符添加一行

user_input = "A very very long string from user input" #1
char_count = 0 #2
new_input_list = [] #3
user_new_input = '' #4
for c in user_input: #5 
        char_count += 1 #6 
        new_input_list.append(c) #7 
        if char_count == 20: #8 
            new_input_list.append('\n') #9 
            char_count = 0 #10 
print(user_new_input.join(new_input_list)) #11 

#1 user_input can't use input because that is a reserved word in python 
#2 used to keep track of number of chars looped through
#3 list to append the chars looped through and the '\n' (new line)
#4 used to hold the joining of the chars and new line in new_input_list
#5 for loop to loop through the user_input string
#6 counts the number of chars by counting the number of loops
#7 appends chars to list
#8 enter condition that once 20 loops(chars) have passed
#9 appends new line to list
#10 resets the char_count to 0 so condition can be used on the next 20 chars
#11 prints out the joined list to a string called user_new_input


你试过什么代码?您当前获得的输出有什么问题?如果您使用的是
'\n'.join()
,您可能也希望在末尾添加一个最后的
'\n'
。在问题中,有这样一个条件“在20个字符之后添加一个换行符”。为了完全正式,我们需要检查最后一个拆分器字符串是否有20个字符,然后才添加
\n
。不管怎样,我们是否需要它还不清楚。这是正确的观点。不过,鼓励用换行符结束所有行可能是个好主意。两种方法都投了赞成票!在任何情况下,只需在这里添加
+'\n'
就可以了(如果是这样的话。干得好!老兄……我知道这可能需要更长的时间才能运行(现在有点想测试一下),但它又好又简洁!也很有可读性。唯一的问题是它省略了结尾处剩下的所有字符。你知道有没有办法用这种方法得到最后一个字符吗?@CrazyChucky,你说得对!我已经更新了答案,我想它现在工作正常了。很好!我以前从未使用过这样的重复范围关于这个问题,我并不经常看到一个答案,因此它就成为我最喜欢的基本习惯用法。它可读性很强,下次我需要拆分字符串时,除非我在优化瓶颈,否则我认为这是一种方法。
user_input = "A very very long string from user input" #1
char_count = 0 #2
new_input_list = [] #3
user_new_input = '' #4
for c in user_input: #5 
        char_count += 1 #6 
        new_input_list.append(c) #7 
        if char_count == 20: #8 
            new_input_list.append('\n') #9 
            char_count = 0 #10 
print(user_new_input.join(new_input_list)) #11 

#1 user_input can't use input because that is a reserved word in python 
#2 used to keep track of number of chars looped through
#3 list to append the chars looped through and the '\n' (new line)
#4 used to hold the joining of the chars and new line in new_input_list
#5 for loop to loop through the user_input string
#6 counts the number of chars by counting the number of loops
#7 appends chars to list
#8 enter condition that once 20 loops(chars) have passed
#9 appends new line to list
#10 resets the char_count to 0 so condition can be used on the next 20 chars
#11 prints out the joined list to a string called user_new_input