2017-08-31 57 views

回答

1

试试这个:

string = input() 
substring = string[string.find('h') + 1:] 
print(string[:string.find('h') + 1] + substring.replace('h', 'H', substring.count('h') - 1)) 
1

你可以找到h第一和最后的位置和字符串拼接代替

string = input() 
lindex = string.find('h') 
rindex = string.rfind('h') 
buf_string = string[lindex + 1:rindex] 
buf_string.replace('h', 'H') 
string = string[:lindex + 1] + buf_string + string[rindex:] 
0

试试这个:

st=input() 
i=st.index('h') 
j=len(st)-1-st[::-1].index('h') 
st=st[:i+1]+st[i+1:j].replace("h","H")+st[j:] 
print (st) 
0

你可以使用pattern.sub进行回调,以下内容替换全部h通过H当它们是2之间h

mystring = 'I say hello hello hello hello hello' 
pat = re.compile(r'(?<=h)(.+)(?=h)') 
res = pat.sub(lambda m: m.group(1).replace(r'h', 'H') , mystring) 
print res 

输出:

I say hello Hello Hello Hello hello 
相关问题