Reputation: 31
i want to take astring and indeses on that string and to convert these characters on those indexes to upper. this is what I've tried but it doesn't work. it shows "list indices must be integers or slices, not list"
test = 'hello there'
l = list(test)
def capital(s, ind):
for i in s:
i = list(i)
i[ind] = i[ind].upper()
print(s)
capital(l,[1,2,5])
Upvotes: 0
Views: 77
Reputation: 420
People are overcomplicating this in their answers. You shouldn't need to iterate over the string, if you already have a defined list of indexes to capitalize. Here's what you can do, iterating only over the indexes parameter:
def capital(text, indexes):
split_text = list(text)
for i in indexes:
split_text[i] = split_text[i].upper()
return ''.join(split_text)
capitalized = capital('hello there', [1, 2, 5])
print(capitalized)
This would print hELlo there
.
A shorter alternative would be using the function enumerate, since you're working with both values and indexes of a sequence (string):
def capital(text, indexes):
return ''.join(c.upper() if i in indexes else c for i,c in enumerate(text))
This would also work perfectly fine.
Upvotes: 0
Reputation: 2940
see fixes in line. you meant to do this this is the right way to to do it.
in your code i[ind] = i[ind].upper()
. ind is a list! this is an error. see my code below:
test = 'hello there'
l = list(test)
def capital(s, ind):
for i in range(len(s)): #i is one letter - charachter
if i in ind:
s[i] = s[i].upper() #it expects an integer! you have passed a list
print(s)
capital(l, [1,2,5])
Effictive approach
test = 'hello there'
l = list(test)
def capital(s, ind):
for i in ind: #i is one letter - charachter
s[i] = s[i].upper() #it expects an integer! you have passed a list
print(s)
capital(l, [1,2,5])
Upvotes: 0
Reputation: 27567
Here is the corrected way to define a function that takes in a list of characters and a list of indexes, and prints out the list with the specified indexes uppercased:
test = 'hello there'
l = list(test)
def capital(s, ind):
for i in ind:
s[i] = s[i].upper()
print(s)
capital(l,[1,2,5])
Output:
['h', 'E', 'L', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e']
Here is a simpler version that does not require a pre-conversion from a string to a list, it simply takes in a string and a list of indices:
def capital(s, ind):
s = ''.join([c.upper() if i in ind else c for i,c in enumerate(s)])
print(s)
capital('hello world',[1,2,5])
Output:
hELlo world
Upvotes: 0
Reputation: 146
It doens't seem correct to iterate over each character of the string. A more correct approach would be to iterate on each index in ind
and capitalize those characters.
Anyway this can be one with a one-liner using a list comprehension:
def capital(s, ind):
capitalized = [s[i].upper() if i in ind else s[i] for i in range(len(s))]
return ''.join(capitalized)
Upvotes: 1
Reputation: 1251
I think you mixed up the list you pass as a second parameter in your capital
function and the list of caracters you have created.
Here is a way to properly write your function :
test = 'hello there'
def capital(s, ind):
for i in ind:
s[i] = s[i].upper()
return(s)
print(capital(list(test),[1,2,5]))
Upvotes: 0