0
Write a Python program to get a string from a given string where all occurrences of its first char have been changed to '#x27;, exc
def word(str1): i=1 b=len(str1) if str1[0] in str1[1:]: for i in range(1,b): if str1[0]!=str1[i]: pass else: str2 = str1.replace(str1[i],"
quot;) print(str2) else: print("No repetition of first char in " + str1) Output: >>> word(a) $esta$t Why is the first char also changing even if the range is specified from 1 to the length of the string? If any answer available please do explain me the changes , it would be very helpful.1 Odpowiedź
+ 1
Try this:
def word(str1):
if str1[0] in str1[1:]:
str2 = str1[0] + str1[1:].replace(str1[0], '#x27;)
print(str2)
else:
print("No repetition of first char in " + str1)