This community-built FAQ covers the “Make Spoonerism” exercise from the lesson “Code Challenge: String Methods”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Learn Python 3
FAQs on the exercise Make Spoonerism
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply ( ) below!
Agree with a comment or answer? Like ( ) to up-vote the contribution!
Need broader help or resources ? Head here .
Looking for motivation to keep learning? Join our wider discussions .
Learn more about how to use this guide.
Found a bug ? Report it!
Have a question about your account or billing? Reach out to our customer support team !
None of the above? Find out where to ask other questions here !
catower
Split this topic
January 7, 2020, 1:44am
3
15 posts were split to a new topic: Sharing Solutions
Hello,
Could someone tell me, for the code below:
def make_spoonerism(word1,word2):
A=word1[0]
B=word2[0]
C=word1[1:]
D=word2[1:]
return B+C, A+D
print(make_spoonerism(“Codecademy”, “Learn”))
Outputs:
(“Lodecademy”, “Cearn”)
How to I tweak the code so the output reads:
Lodecademy Cearn
(ie. without the brackets and quotation marks)
Thankyou
1 Like
ionatan
January 29, 2020, 11:16pm
5
If you have two strings, how can you combine them into one?
Actually you have three strings, you need a space as well.
Try +
… you already used that, even.
2 Likes
Ahhh - thankyou - appreciated.
1 Like
mtf
March 28, 2020, 5:53am
8
Only problem with that swap is that it will make the first letter of both words the same.
2 Likes
mtf
March 28, 2020, 6:02am
10
A swap takes three steps.
# given A and B
A = 6
B = 7
To swap the two values we need to hold one of them temporarily.
C = A
A = B
B = C
The logic is simple. Once A becomes B, how can B become A?
print (A) # 7
print (B) # 6
2 Likes
I get it now
Thank you Sir
2 Likes
hkngry
November 3, 2020, 8:37am
15
Hello, my solution below:
def make_spoonerism(word1, word2):
string1, string2 = " ", " "
string1 = word2[0] + word1[1:]
string2 = word1[0] + word2[1:]
last_string = string1 + " " + string2
return last_string
def make_spoonerism(word1, word2):
new_word1 = word1.replace(word1[0], word2[0])
new_word2 = word2.replace(word2[0], word1[0])
return new_word1+" "+new_word2
Another solution:
def make_spoonerism(word1, word2):
spooned_word1= word2[0] + word1[1:]
spooned_word2= word1[0] + word2[1:]
return "{} {}".format(spooned_word1, spooned_word2)
Hello my solution
def make_spoonerism(word1, word2):
return word2[0] + word1[1:] + " " + word1[0] + word2[1:]
Well done everyone - really succinct code. For some reason I tried to do this without splicing - just for the fun of it I guess. I recommend trying this challenge if you’re learning to code :3
def spoonerism(string1, string2):
emptylist = []
emptylist2 = []
for index in range(len(string1)):
if index == 0:
emptylist.append(string2[0])
else:
emptylist.append(string1[index])
for index in range(len(string2)):
if index == 0:
emptylist2.append(string1[0])
else:
emptylist2.append(string2[index])
firststring = "".join(emptylist)
secondstring = "".join(emptylist2)
output = firststring + " " + secondstring
return output
1 Like
You could shorten it up a bit, I don’t know if the course has gotten into comprehensions.
def spoonerator(w1, w2):
wl1 = [char for char in w1] + [" "]
wl2 = [char for char in w2]
wl1[0], wl2[0] = wl2[0], wl1[0]
return "".join(wl1) + "".join(wl2)
2 Likes
My solution is a little different. Its a one sencance function that returns the value straight away:
def make_spoonerism(w1, w2):
return w2[0] + w1[1:]+ " " + w1[0] + w2[1:]
Can somebody please break this down for me?
def make_spoonerism(word1, word2):
return word2[0]+word1[1:]+" "+word1[0]+word2[1:]
print(make_spoonerism(“Codecademy”, “Learn”))
I don’t understand how this returns Cearn. What happens to the space?
word2[0] =
word1[1:] =
" " = space
word1[0] =
word2[1:] =