Issue
This is my string:
keyword = "qatarworldcup"
I mean my string should be qqatarworldcup
, qatarworldcupp
or qatarrworlddcup
Solution
Iteration through index-character pairs, apply the condition on each "term" with the ternary operator and join everything together.
import random
# fix random number generator (for testing only!)
random.seed(190)
keyword = "qatarworldcup"
# random index
r = random.randint(0, len(keyword)-1)
out = ''.join(char * 2 if i == r else char for i, char in enumerate(keyword))
print(out)
#qaatarworldcup
Answered By - cards
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.