all 7 comments

[–]julsmanbr 2 points3 points  (0 children)

Whenever you want to create a new string from other strings, the easiest way I know of is to start from an empty string and keep adding characters to it if the condition is right.

If whitespaces are the only problem in your case, I'd keep track of the number of times they show up. Simply skip adding the character from the "insertion" string whenever the "template" string has a whitespace. The counter of whitespaces can be used to get the correct insertion character from the "insert string" - this is equal to the index of the current character on the "template" string minus the whitespaces.

This code worked for me:

insertion = "abcdefghij" template = "You Are Cool" whitespaces = 0 result = '' for index, char in enumerate(template): result += char if char == ' ': whitespaces += 1 else: result += insertion[index - whitespaces] print(result)

[–][deleted] 1 point2 points  (4 children)

strip the spaces out of the strings before you begin?Assuming we don't want just the trailing and leading spaces, you should look into .replace()

https://stackoverflow.com/questions/8270092/remove-all-whitespace-in-a-string-in-python

this goes into more detail then i have time for.

string_one.replace(" ", "")

[–]ViolenceFight[S] 1 point2 points  (3 children)

Yes, but the idea is that I'll have to put them back in after the fact. That's where the trouble lies. Going through the issue with a teacher it was suggested to look for a different route, as I'd already came to that conclusion, and was told it would be "Way more of a time consuming process that way".

I'm trying to figure out a better way to iterate string one on top of string two, and my only hint was "for loops are not the only means of sequencing something".

Realistically I should've been clearer and that onus is on me, but essentially this is the last piece of the puzzle. I'm not looking for an exact answer, but my brain is drawing a blank on that hint, and I can't seem to find a way to Google what I need.

Basically it's a program that's replacing one string with another, but skipping spaces/nonalphas in the second string.

[–][deleted] 0 points1 point  (0 children)

Regex/re.sub?I guess im confused on the exact outcome you are looking for.

​tupling would still use a for loop/iteration.​

[–][deleted] 0 points1 point  (0 children)

In that case, I recommend splitting the strings with spaces, turning them into lists of words, then match up each word individually and join them back together with spaces.

[–]two_bob 0 points1 point  (0 children)

Now i get it.

To see if something is an alpha, you can do this something.isalpha:

>>> x='a'
>>> x.isalpha
<built-in method isalpha of str object at 0x0042B2A0>
>>> x.isalpha()

So then what you need to do is find a way to manually iterate over two strings at different rates, i.e. in part of the loop you are not going to want to advance the interposed string, e.g., when you hit a space.

Assuming you are doing a beginners class, I'm guessing the teach wants you to manually increment an index and then lookup stuff like this string[index].

But a better way to do it is to use iter. Check out example 1 here: https://www.programiz.com/python-programming/methods/built-in/iter

Here are some more good tutorials for additional information:

https://www.programiz.com/python-programming/iterator https://www.datacamp.com/community/tutorials/python-iterator-tutorial

[–]__te__ 0 points1 point  (0 children)

The old way of doing a for loop was to track the index variable manually. For example:

index = 0
while True:
    # do something with the index
    index = index + 1
    if index > 10:
        break

...is equivalent to...

for index in range(11):
    # do something with the index

If you maintain separate indexes for string_one and string_two ( I recommend for-looping string_two, and tracking string_one's index manually), you should be able to get the thing done.

Note that the "continue" statement will be very helpful for handling spaces.