0

How do I turn the number strings into integers? I need the numbers made by this code to be integers, not strings.

def capital_indexes(big):
bog = "["
bug = 0
for letter in big:
    if letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
        bog = bog + str(bug) + ", "
        bug = bug + 1
    else:
        bug = bug + 1
bog = bog[:-2]
bog = bog + "]"

return bog
Ron Cruz
  • 1
  • 1
  • What language is this? – Don Branson Dec 03 '21 at 01:53
  • 2
    This language is Python. – Ron Cruz Dec 03 '21 at 02:01
  • I googled and found https://careerkarma.com/blog/python-string-to-int/ – Don Branson Dec 03 '21 at 02:02
  • 2
    Does this answer your question? [How do I parse a string to a float or int?](https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int) – Don Branson Dec 03 '21 at 02:15
  • Yes, but if you input a word to capital_indexes, say, "HeLlO", it would return [0, 2, 4], with 0, 2, 4 being strings. If I use the int() method, it would say that I cant turn [ and , to an int. I want to turn only the numbers into integers. – Ron Cruz Dec 03 '21 at 02:26
  • Strings have an `.isdigit` method that you can use when iterating over the string character by character. Maybe you are looking for the 1-liner `sum(int(c) for c in big if c.isdigit())` – John Coleman Dec 03 '21 at 02:34

1 Answers1

0

I guess you want to mean your output should be in python list not in str.

Here is solution how to print list:

def capital_indexes(big):
    output = []
    for i in range(len(big)):
        if big[i] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
            output.append(i)
    return output

Output:

>>> capital_indexes("aBrLT")
>>> [1, 3, 4]