I want to create a function that capitalizes a string according to the array that is provided. My current function takes in 2 arguments: the string you want to change and an array which would correspond to the letters you want to change. My function looks like so:
def capitalize[s, ind]:
for i in ind:
try:
s = s.replace[s[i], s[i].upper[]]
except IndexError:
print["Sorry, index is not range!"]
return s
This function seemed to work at first, when I tried a few examples such as:
capitalize["abc",[1,2]]
#Output: aBC
However, I cannot work out why this example fails:
capitalize["abracadabra",[2,6,9,10]]
#Output: AbRAcADAbRA - should be abRAcADAbRA
Why is my first letter suddenly becoming capitalized? Any help would be deeply appreciated.
Creativecommons và DMCA
Bài viết liên quan
Cắt chuỗi trong python [slice string python]
Nối chuỗi và lặp chuỗi trong python
Cách sử dụng chuỗi thô raw string trong python
Ký tự đặc biệt trong python [escape sequence]
Căn trái, căn giữa và căn phải một chuỗi trong Python [ljust, center, rjust, zfill]
Phương thức Center[] trong Python
Chuyển chữ hoa thành chữ thường và ngược lại trong python
Chuyển list thành string trong Python