I was playing in the REPL with this function and can't figure out why word2 returns an empty string after join method?
Basically as I stated. In the REPL word1 joins into 'str' just like i was hoping, but word2 returns "". If it's the same logic what is causing this?
def sillycase(string): string = list(string) half = int(len(string)) // 2 word1 = string[:half] word1 = "".join(word1[:half]).lower() word2 = string[half:] word2 = "".join(word2[half:]).upper() return word1 + word2
Steven Parker171,169 Points
You don't need the join, but what's happening here would be the same with or without it.
When "word2" is first assigned, it gets "half" of the iterable: "
word2 = string[half:]"
Then, on the next line, the "join" starts with taking another slice: "
But if the first slice made it only "half" long, then another slice that has a start value of "half" would not have any content.
Kars Jansens4,370 Points
I don't kwow why you should use "join". You can do this:
def sillycase(string): half = int(len(string)) // 2 word1 = string[:half].lower() word2 = string[half:].upper() return word1 + word2