【发布时间】:2018-05-14 19:13:06
【问题描述】:
我知道这个函数会围绕字符串中的字符移动,例如:
def swapping(a, b, c):
x = list(a)
x[b], x[c] = x[c], x[b]
return ''.join(x)
这让我可以这样做:
swapping('abcde', 1, 3)
'adcbe'
swapping('abcde', 0, 1)
'bacde'
但是我怎样才能让它做这样的事情,所以我不只是在字母周围移动?这就是我想要完成的:
swapping("Boys and girls left the school.", "boys", "girls")
swapping("Boys and girls left the school.", "GIRLS", "bOYS")
should both have an output: "GIRLS and BOYS left the school."
# Basically swapping the words that are typed out after writing down a string
【问题讨论】:
-
@DeepSpace,不太像……
-
如果出现多次怎么办?
标签: python python-3.x