Upper only letters that I have indexes on the list (PYTHON)
s = "string"
l = [1,3]
# output is: sTrIng
Tried this but it wont work
for i in l:
s[i] = s[i].upper()
CodePudding user response:
Strings are immutable. If you want to check by index, you can for example use a list and update the value by index (and check if that index exists first).
Then at the end, join the characters to a string.
s = "string"
result = list(s)
lst = [1, 3]
length = len(result)
for i in lst:
if i < length:
result[i] = result[i].upper()
print("".join(result))
Output
sTrIng
CodePudding user response:
This solution works
s = "string"
l = [1,3]
for i in l:
newStr = s[i]
print(newStr.upper())