4

I'm aware that this replacing characters question may have already been posed and I've checked them out, but they still don't answer my question. I'm also new to python, so my understand isn't all that amazing.

def stringMix():
    MixA = print (userStringA.replace([0:2],userStringB[0:2]))
    MixB = print (userStringB.replace([0:2],userStringB[0:2]))

userStringA = input("Please enter a string consisting of over two characters ")
userStringB = input("Please enter a second string consisting of over two characters ")

print (userStringA)
print (userStringB)

print (MixA, MixB)

That's my code so far, however when I run it, it poses a syntax error highlighting the colon. I simply want to replace the first two characters of userStringA with the first two characters of userStringB, and vice versa.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
The White Wolf
  • 65
  • 1
  • 2
  • 8

4 Answers4

3

you are not using method calls properly. ie, you are defining stringMix as a function, but using variables that are out of the scope of the function. I think what you are trying to do is:

def stringMix(a,b):
  print (a.replace([0:2]b[0:2]))
  print (b.replace([0:2],a[0:2]))

userStringA = input("Please enter a string consisting of over two characters ")
userStringB = input("Please enter a second string consisting of over two characters ")

print (userStringA)
print (userStringB)

stringMix(userStringA,userStringB)

However, as previous answers and comments suggest, str.replace is not really the way to do this. you should instead do:

def stringMix(a,b):
  print (a[0:2]+b[2:])
  print (b[0:2]+a[2:])

to take advantage of string slicing and concatenating

R Nar
  • 5,465
  • 1
  • 16
  • 32
  • Ah I understand, you're simply printing the string without the first two characters rather than using the replacing mechanic. Thanks a lot! – The White Wolf Oct 19 '15 at 18:58
  • yes, since you only need to print it and dont need to maintain the values. if you do, however, you can have something like `a = b[0:2] + a[2:]` – R Nar Oct 19 '15 at 19:00
  • 1
    @TheWhiteWolf: The reason `str.replace` is wrong is that it will replace all occurrences: `'fifty-five'.replace('fi', 'xx')` gives `'xxfty-xxve`. – Steven Rumbalski Oct 19 '15 at 19:10
2

Simply do:

print userStringB[:2] + userStringA[2:]

and

print userStringA[:2] + userStringB[2:]
Muhammad Tahir
  • 5,006
  • 1
  • 19
  • 36
  • Wow, that actually worked! But how does that work? Please explain, thanks – The White Wolf Oct 19 '15 at 18:54
  • This is called slicing in Python. `userStringB[:2]` will return `userStringB`'s first 2 characters and `userStringA[2:]` returns `userStringA`'s all the characters except first 2 and then we concatenate both the returned strings to create result string. – Muhammad Tahir Oct 19 '15 at 19:02
  • Please see this link for detailed description http://stackoverflow.com/questions/509211/explain-pythons-slice-notation – Muhammad Tahir Oct 19 '15 at 19:04
1
userStringA,userStringB = userStringB[:2] + userStringA[2:],userStringA[:2] + userStringB[2:]

I guess ...

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

replace is a method you call if you want python to search for a substring in a string, and then replace it with another string. What you want to do is simply

MixA = userStringA[0:2] + userStringB[2:] #from 2 till end
MixB = userStringB[0:2] + userStringA[2:] #from 2 till end
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94