【发布时间】:2020-04-16 03:36:45
【问题描述】:
这是我的任务
journey = """Just a small tone girl
Leaving in a lonely whirl
She took the midnight tray going anywhere
Just a seedy boy
Bored and raised in South Detroit or something
He took the midnight tray going anywhere"""
毛。好的,对于这个练习,你的工作是使用 Python 的字符串替换方法来修复这个字符串并将新版本打印到控制台。
这就是我所做的
journey = """ just a small tone girl
Leaving in a lonely whirl
she took a midnight tray going anywhere
Just a seedy boy
bored and raised in south detroit or something
He took the midnight tray going anywhere"""
journeyEdit = journey.replace("tone" ,
"town").replace("tray","train").replace("seedy","city").replace("Leaving",
"living").replace("bored","born").replace("whirl","world").replace("or
something", " ")
print (journeyEdit)
【问题讨论】:
-
有更短的方法吗?
-
嗯,这有点短,但事实是,它效率不高。你可以要求一种我认为你可以做到的有效方法。
-
使用字典?
y = d.get(x, x)
标签: python str-replace