python 2.7 - How to double a char in a string? -
i trying write function takes 2 arguments, string , letter. function should double number of letter in string. example:
double_letters("happy", "p") happppy
what have done far;
def double_letter(strng, letter): new_word = "" char in strng: if char == letter: pos = strng.index(char) new_word = letter+strng[pos:]
but giving me output: pppy
how can change function output: happppy?
use string.replace
string = 'happy' letter = 'p' string = string.replace(letter, letter + letter) print string
Comments
Post a Comment