-1

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:

elloH

But my program returns it as

elloh

What I want to know is, how can I get a list with the indices of where a capital letter is, so I can make the letters at that same spot in the new string uppercase?

I think you would do it by converting the String to a list through list(string) and then iterate through the list so, and whenever the item in the list returns true for isUpper(), then store the index of that in a list. I just am not able to write it in python.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
CyanogenCX
  • 414
  • 6
  • 17
  • 1
    Can you add the code you use to do this currently? – Anshul Goyal Feb 09 '15 at 03:45
  • possible duplicate of [how can i check if a letter in a string is capialized using python?](http://stackoverflow.com/questions/4697535/how-can-i-check-if-a-letter-in-a-string-is-capialized-using-python) – paisanco Feb 09 '15 at 03:47
  • @paisanco no its not a duplicate. I already wrote in my question if you read it that I do know how to check with isUpper() – CyanogenCX Feb 09 '15 at 03:49
  • @mu無 the code I am using is from Ashwini's answer [here](http://stackoverflow.com/questions/14424500/text-shift-function-in-python), just slightly modified for a different task – CyanogenCX Feb 09 '15 at 03:49
  • Go ahead and paste it in. It'll make this question easier to answer. – paulmelnikow Feb 09 '15 at 04:19
  • Nothing to do with regular expressions, so I removed the tag. However, your question is not specific enough for the solution you want. – Burhan Khalid Feb 09 '15 at 04:25

2 Answers2

9

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:

def shift(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:

def getindices(s):
    return [i for i, c in enumerate(s) if c.isupper()]

Examples:

>>> getindices('Hello')
[0]
>>> getindices('HeLlO')
[0, 2, 4]
John1024
  • 109,961
  • 14
  • 137
  • 171
  • That fits my purpose for this problem I have, but doesn't answer the question I have (Get the indices of capital letters in a string), so I'll +1 this, but won't accept yet. – CyanogenCX Feb 09 '15 at 03:53
  • 1
    @CyanogenCX don't you think the best answer is to eliminate the problem completely before it even comes up? This answer does that. I'm having trouble conceiving how you got your original result, since you didn't share any code. – Mark Ransom Feb 09 '15 at 03:55
  • @CyanogenCX OK. I added a solution for getting indices of capital letters. – John1024 Feb 09 '15 at 03:57
  • @MarkRansom No, I think the best answer is one that answers the question I asked. Which John has done now. I just put the background about my current problem to give some context, but the goal of this question was not to solve the problem, but to find the answer to the question I asked in the title. – CyanogenCX Feb 09 '15 at 03:58
2

To add to the solution by @John1024

This supports any number to rotate by

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

>>> shift('Hello', 51)
'elloH'
jamylak
  • 128,818
  • 30
  • 231
  • 230