Reputation: 71
I want to capitalise every 5th letter of name
Note: i only want to use .upper() method "not capitalize()"
I tried this but im getting IndexError: string index out of range
name = "Cristiano Ronaldo"
for t in range(0,len(name),5):
name = str(name)[t].upper()
print(name)
Upvotes: 0
Views: 129
Reputation: 1012
"".join([char.upper() if index > 0 and (index - 1) % 5 == 0 else char for index, char in enumerate(name)])
Upvotes: 3
Reputation: 1907
Use pythonic
way:
name = "cristiano Ronaldo"
print(''.join([l if i%5 else l.upper() for i, l in enumerate(name, 1)]))
Upvotes: 0
Reputation: 743
name = "Cristiano Ronaldo"
print(''.join([ letter.upper() if (index+1) % 5 == 0 else letter for index,letter in enumerate(name)]))
enumerate
method used to track the index of each letter in string while looping and for every multiple of 5 we are doing letter.upper()
else letter
Upvotes: 0
Reputation: 2585
you can first change the str
to list
:
l_name = list(name)
for i in range(0,len(l_name),5):
l_name[i] = l_name[i].upper()
result = ''.join(l_name)
Upvotes: 0