1

I'm just getting back into python, and I'm trying to search over two characters at a time through a string, and replace them with something else. Such as searching through the string "aah" for the string "aa" and replacing it with something else, such as "xx" but when I try to run it, it says type must be a string, not int. This is my code, any help would be greatly appreciated, thanks

test = input("enter words to encrypt\n")
#print(test)

#change a letter
def changea(test):
    #print(test)
    outp = ""
    for i in test:
        if i & i+1  == "aa":
            outp += "x"
        else:
            outp += i

    print(outp)
changea(test)
Joel Banks
  • 141
  • 1
  • 11
  • 1
    the logical and operator in Python is the keyword `and`. `&` is the _bitwise_ and operator. – Christian Dean Jun 09 '18 at 02:09
  • 1
    To address your main problem, `i+1` fails because `i` is a string from the string `test`, and `1` is an integer. You probably meant to _index_ the string with the integer, rather than directly attempt to add the integer and string. – Christian Dean Jun 09 '18 at 02:11
  • Related - [Iterate over all pairs of consecutive items in a list](https://stackoverflow.com/q/21303224/2823755) - This accepted answer would work for a string also. – wwii Jun 09 '18 at 02:13
  • you can use if test.count("a") == 2: # do something – Hozayfa El Rifai Jun 09 '18 at 02:13
  • 1
    You may find [`str.replace`](https://docs.python.org/3/library/stdtypes.html#str.replace) useful – bla Jun 09 '18 at 02:13
  • @ChristianDean I changed that line to if test[i] and test[i+1] == "aa": but I still get an error that says string indices must be integers. How would I go about changing this? – Joel Banks Jun 09 '18 at 02:20
  • `if test[i] and test[i+1] == "aa"` checks whether `test[i+1] == "aa"` **and** `test[i]`. The latter condition is always true for printable characters. You need `if test[i]=="aa" and test[i+1] == "aa"`. Also, the loop header must be changed, too. – DYZ Jun 09 '18 at 02:21
  • Is `i` not a string @DyZ? How would you index a string `test` with a string `i`? – Christian Dean Jun 09 '18 at 02:22
  • @ChristianDean I posted an unfinished comment. Yes, that's another problem with the OP's code. – DYZ Jun 09 '18 at 02:23
  • @DyZ So how would I change I so that it would work? – Joel Banks Jun 09 '18 at 02:36
  • Well, you have two answers down there, choose what you want. – DYZ Jun 09 '18 at 02:37

2 Answers2

3

Ideally, you should use replace or re.sub. But if you are committed to using a loop, here's what you may try:

def changea(test):
    pairs = zip(test, test[1:] + "$")
    out = ""
    for x, y in pairs:
        if x == "a" and y == "a":
            out += "x"
            next(pairs) # Skip the next iteration
        else:
            out += x
    return out

changea("Maary haad aa little laamb")
#'Mxry hxd x little lxmb'
DYZ
  • 55,249
  • 10
  • 64
  • 93
0

The & operator does a bitwise and on two integers. Your for-loop loops over a string so the loop variable i takes successive characters of the string as its values. Hence the error: you are applying the & operator on a character. In addition, i+1 is meaningless: it tries to apply the + operator to a character and an integer - python will complain that you cannot concatenate a character and an integer.

The i & i+1 expression was meant to give you two adjacent characters but it's wrong.

There are a few ways to proceed: one is to loop over the string using indices (which I guess is what you were trying to do, since you named the for-loop variable i, the traditional name for a loop index).

for i in range(len(test)):

will make i an integer ranging from 0 to len(test)-1. Then you need to slice off two characters of the string starting at position i:

for i in range(len(test)):
     if test[i:i+2] == "aa":
         outp += "x"
     else:
         outp += test[i]

You have to remember that the index over a string goes from 0 to len(string) - 1 and you also have to remember that the slice test[i:j] is the piece of the string between indices i and j-1 (NOT j).

This is not particularly idiomatic python, but I hope it's understandable.

NickD
  • 5,937
  • 1
  • 21
  • 38