Joining characters when iterating over a string

python

#1



I've been stuck on this problem for a while. I love CodeAcademy, but I've finished the courses I'm interested in so I've started using other sites to continue practicing. I still use CodeAcademy labs to run my code, but now that I'm running into problems, I need help and I'm not comfortable posting on the elitist StackOverflow (had a bad experience).

Basically, I can't figure out how to take out the white space between characters as I iterate over a string.
In the example below, I'm trying to iterate over a string and switch the case from upper to lower or vice versa. Right now my code prints out the right cases, but with a space between each character. I've tried looking up solutions on StackOverflow and I've seen people suggesting removing white space with "".join() or str.split() but I can't seem to figure it out. Any help would be great :slight_smile:


def to_alternating_case(string):
    result = ''
    for x in string:
            if x.isupper():
                print x.lower(),
                "".join(string)
            else:
                print x.upper(),
                "".join(string)

EDIT: This is how I edited my code that passed:

def to_alternating_case(string):
    result = ''
    for x in string:
            if x.isupper():
                result += x.lower()
            else:
                result += x.upper()
    return result


#2

.join() is designed to have a list (["i","am","a","list"]) to join the list together:

store = ["i","am","a","list"]
print " ".join(store)

if you have a string, you can simple append the items with a +=:

string = "hello world"
result = ""
for x in string:
   result += x

Now, this program i wrote is a bit rubbish (t doesn't do anything useful), but i hope it gives you an idea


#3

Oh, i am not reading your question properly. Well, with what i already explained it is rather easy, you insert a if condition:

if x is " ":

and then, instead of x, you append an empty string to it:

if x is " ":
   result += ""

or you simple tell it to do nothing:

if x is " ":
   pass

seems that gives the same result


#4

I'm sorry, I'm into the learning process yet (69% of the course), but the problem of to extract the white space (" ") shouldn't be wrote as the solution below intead of the condition above?

if x is " ":
        x = ""

#5

but i assume that by extracting the white space, you meant that hello world should become helloworld, in which case, you need to append to either add a empty string (""), or pass, or only add if it is not a space:

if not x is " ":
    result += x

this:

if x is " ":
        x = ""

will also work, but purely because you are not adding something to result string (it is basically the same as using pass.

I thought you simple wanted to prevent spaces from being added to your result string.

You don't seem to understand what this means:

if x is " ":
        x = ""

it will set x to nothing, but this isn't stored anywhere, it is forgotten when the loop will run again. You can't simple remove spaces like that from a string, it is not that simple.


#6

That makes a lot of sense, actually - and it worked! Thank you.

Now that I think about it, I'm pretty sure I learned that in the Python course but have since forgotten. :sweat_smile:

Thanks again!


#7

I was trying to follow your samples above. I've written first your initial conditional:

then I tried both solutions, of your second sample above, inside the initial condition. And it stayed like that:

string = "hello world"
result = ""
for x in string:
	if x is " ":
		pass # or ' that: result += "" "
	result += x
print  result

but both results it was the same:

"hello world"

only when I changed those for this condtion x = "" I achieved the expected result:

"helloworld"

That is why I am asking


#8

Waahh, i am helping two people at once... Confusing. Well.

@orangepony, di you understand and does it make sense?

@blogmaster36220, well, now your rendered the if statement useless, use else:

if x is " ":
    pass
else:
    result += x

now, only if it is not a space, the letters will be added to the result string


#9

@stetim94 haha yes, thank you. I replied to you first answer on this thread. Sorry for the resulting confusion :stuck_out_tongue:


#10

Good to hear that you resolved the issue :slight_smile: it was getting even more tricky because in this bit:

if x is " ":
   pass

i forgot the else clause


#11

Thanks! Now I got you said.


#12

yea, otherwise the if clause has no effect, because you where appending either way

Good to hear your issue is also resolved :slight_smile:


#13

Yeah no worries! 90% of the time the answer is easier than I'm making it to be, and someone just has to poke me on the shoulder and whisper, "Hey, dummy - that's too hard. Remember this thing you already learned?" and I remember. So thanks!


#14

I know the feeling. Yea exactly, you understand it. So just need a little bit of help, some people don't understand, and need a little more then just a little push :slight_smile: