Python Python Collections (2016, retired 2019) Slices sillyCase

Jason Smith
Jason Smith
5,736 Points

Can anyone help? i'm still not sure how i should go about turning a string into a list, then back again.

i can't seem to figure this out
def sillycase(silly):
    newlist = list(silly)
    half = int(len(silly))//2

2 Answers

Jason, You've made a few errors here. First, no need to convert the string to a list. Then when calculating the value of half you do integer division (//) after converting the string length into an integer with int( ) (again, no need, as len will always return an integer). Finally, the string functions upper( ), lower( ) etc. . . do not take arguments. See my solution below:

 def sillycase(string):                             
    half = len(string)//2                   #  calculate half point in string
    first_half = string[:half].lower()      #  use half to slice string and convert to lowercase
    second_half = string[half:].upper()     #  use half to slice string and convert to uppercase
    return first_half + second_half         #  concatenate half strings to return full modified string

Hope this helps

Oleg Zhoglo
Oleg Zhoglo
3,455 Points

What's the difference between / and //?

Hi Oleg,

Division with / will return a float, while // will always return an integer (any fractional remainder is discarded, so this is not like using the math function round, which although returning an integer, will round up or down depending on the fractional remainder). Think of // as the complement to modulo division using % which only returns the remainder.

Jason Smith
Jason Smith
5,736 Points

thanks, that looks alot easier to understand. i didn't know len() always returned an integer, either!