2021年4月8日星期四

Replacing characters in a word from the specified position to the end in a while loop

I need to print output like this:
"jav-" for 'java'
"pyt---" for 'python'
"kot---" for 'kotlin'
"jav-------" for 'javascript'

For 'python' and 'kotlin' it prints as expected, but for 'java' or 'javascript' it prints "j-v-" and "j-v------".

word = ['python', 'java', 'kotlin', 'javascript']  import random  x = random.choice(word)  print("old value of X >", x)  lamai = len(x)  change = 3  symbol = "-"  while lamai > change:      y = x.replace(x[change], symbol)      x = y      change = change + 1      print("this is y", y)  

visualisation of code

https://stackoverflow.com/questions/66986088/replacing-characters-in-a-word-from-the-specified-position-to-the-end-in-a-while April 07, 2021 at 08:28PM

没有评论:

发表评论