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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -