Define a function replaceCharAtPos(orig,pos) which receives two input parameters, a string (orig) and a positive integer number (pos).
If the number is a position within the string, the function should return a new string. The new string should be the original string except that instead of the original character in that position pos, it should have the position number itself (if the position number has more than 1 digit, the character should be replaced by the digit in the units part of the position number, e.g. 2 if the position number is 12).
If the number pos is not a valid position within the original string orig, the string returned should be exactly the same as the original string.
replaceCharAtPos('abcd',1) should return 'a1cd'
replaceCharAtPos('abcd',10) should return 'abcd'
replaceCharAtPos('abcdefghijklmn',12) should return 'abcdefghijkl2n'
As an example, the following code fragment:
should produce the output:
I'm not sure if this is your cs course hw but I'd suggest trying it yourself first.
Anyway, here's the function:
def replaceCharAtPos(s, i): if i > (len(s)-1): return s s = list(s) s[i] = str(i) return ''.join(s)