-3

I want to make a new string made up of every other character with the first string. Then I want the first string to only have the letters that aren't in the new string. Thanks if you can help me out.

str = 'Hello'
str2 = str[::2]
for char in str:
   if char in str2

I was doing well until I got stuck on how to remove the letters in the first string.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70

2 Answers2

-1

You can try manually going through every character and checking if it's not in the other string, which is what you appeared to have been trying. To achieve this, you can use if char not in str2 instead. As this checks if the character is NOT in the other string. So your code would look somewhat like this:

str = 'Hello'
str2 = str[::2]
for char in str:
   if char not in str2:

Now, we need to somehow remove this character. I would personally suggest just creating a new string with your new letters, so your completed code would look somewhat like this:

str = 'Hello'
str2 = str[::2]
result = '' # Empty string

for char in str:
   if char not in str2: # If the character is not in the other string
      result += char # Add the character to the result

print(result) # Print the result

realhuman
  • 137
  • 1
  • 8
  • 1
    @TimRoberts the goal is to have a string with the characters that are NOT in the second string, not the even characters – realhuman Dec 14 '22 at 06:16
-3
    string = 'Hello123µ►'
    # every other character (even)
    string_evens_set = string[1::2] 
    string_odds_set = string[0::2]
    dict_evens = {}
    for c in string_evens_set:
        if c >= 'a' and c<='z' or c >= 'A' and c<='Z':
            dict_evens[c] = True
    list_result = []
    for c in string_odds_set:
        if c >= 'a' and c<='z' or c >= 'A' and c<='Z':
            #if not dict_evens[c]:
            if not c in dict_evens.keys():
                list_result.append(c)
    the_first_string = "".join(list_result)
    print(the_first_string)

Well if that is not meant as: 1: select every other character 2: select not (1)

Therefore simplyfyable as "select 1,3,5,7.... character", because they are not in the second string.
second string contains 0,2,4,6,8.... characters.

Then maybe there is much wider meaning than suggested. For example, that user meant

  1. only letters (not digits, not local characters, not control symbols) [a-zA-Z]
  2. that are not in the new string (subset of every 2nd character)
  • if such is the case:
string = 'Hello123µ►'
# every other character (even)
string_evens = string[1::2] 

for character in 

# every oddly numbered character
# string = string[::2] 

    string = 'Hello'
    # every other character (even)
    string = string[1::2] 
    # every oddly numbered character
    # string = string[::2] 
    print(string)

you can do that in single line operation - assign to the string the selection you already did..


    result:  [H]e[l]l[o]
    visible: el

P.S. please take in account the comments especially about immutability if you plan to use this with blobs or any other large strings.

P.S.S if you are going to use reserved words as variable names then no wonder you might just encounter some errors. That is reason why I do not use str as variable name. You can look up the reserved words in any programming language like for, if, else, data type names etc..

wilpeers
  • 1
  • 2