Python 如何在一个字符串中查找字母、数字,然后在Pyton中添加换行符

Python 如何在一个字符串中查找字母、数字,然后在Pyton中添加换行符,python,python-3.x,Python,Python 3.x,我很难做到这一点。我不知道也许我不擅长这个,我无法想象代码是如何工作的。 我有一个字符串文件: My mama told me that I am: a. must be an engineer; but b. I want to be a Doctor. 输出为: My mama told me that I am: a. must be an engineer; but b. I want to be a Doctor. 第二个字符串是: (1) My mama told me th

我很难做到这一点。我不知道也许我不擅长这个,我无法想象代码是如何工作的。 我有一个字符串文件:

My mama told me that I am: a. must be an engineer; but b. I want to be a Doctor.
输出为:

My mama told me that I am: 
a. must be an engineer; but 
b. I want to be a Doctor.
第二个字符串是:

(1) My mama told me that I am must be an engineer; but (2) I want to be a Doctor: a. I am must learn Biology. b. That's my dream. c. I can help people.
输出为:

(1) My mama told me that I am must be an engineer; but 
(2) I want to be a Doctor: 
    a. I am must learn Biology. 
    b. That's my dream. 
    c. I can help people.

我尝试使用<代码>。查找然后放“\n”,但是我找不到在字符串中间给出“\n”的方法。有人能帮我吗

虽然SO不是一种编码服务,但我将为您的第一个字符串提供一个提示,您可以轻松地扩展第二个字符串:

s = "My mama told me that I am: a. must be an engineer; but b. I want to be a Doctor."

import re

patterns = re.findall(" [a-z]\. ", s)

i = 0
new_s = ""
for item in patterns:
    j = s.index(item)
    if item == patterns[-1]:
        new_s += s[i:j].strip() + "\n" + s[j: ].strip()
    else:
        new_s += s[i:j].strip() + "\n"
        i = j

print(new_s)

你好欢迎来到SO!请阅读并包含最小可复制示例,以便我们为您提供帮助。您编写的输出,是您想要的输出还是您的程序正在显示的输出?此外,如果您通过文件读取此内容,则有一个函数“f.readline()”,一次读取一行。我希望输出像这样显示,先生。嗯,所以我用“f.readlines()”来读这行,然后把“\n”放在那里?