Skip to content Skip to sidebar Skip to footer

Get The Indices Of Capital Letters In A String

Say for example I have the following string: Hello And my job is to shift all the letters over by however many spots I am told to, and lets say it is 1, so the result would be: el

Solution 1:

I will assume that your question is regarding:

And my job is to shift all the letters over by however many spots I am told to

In that case, try:

defshift(s, n): 
    return s[n:] + s[:n]

Examples:

>>> shift('Hello', 1)
'elloH'
>>> shift('Hello', 2)
'lloHe'

This appears to be the output that you were looking for.

How to get indices of the capital letters in a string:

defgetindices(s):
    return [i for i, c inenumerate(s) if c.isupper()]

Examples:

>>>getindices('Hello')
[0]
>>>getindices('HeLlO')
[0, 2, 4]

Solution 2:

To add to the solution by @John1024

This supports any number to rotate by

defshift(s, n):
    return s[n%len(s):] + text[:n%len(s)]

>>> shift('Hello', 51)
'elloH'

Post a Comment for "Get The Indices Of Capital Letters In A String"