How to make it so that every alternate character in a string appears as a hyphen
Code using a for loop in Python:
word = "Python" #Add your code for random words instead
ans = ""
for i in range(len(word)):
if i%2 != 0:
ans += "-"
else:
ans += word[i]
print(ans)
Output:
P-t-o-
You could get letters at even positions using a striding subscript (word[::2]) and join them with an hyphen. Make sure to have an extra space for the last letter in case the word has an even number of letters.
word = "Elephant"
hyphened = "-".join((word+' ')[::2])
print(hyphened)
E-e-h-n-