-1

I am writing some code, and would like to add a conditional statement such that as we loop through stringg below, if we hit an integer, then ... we do something (doesn't matter what)

stringg = "3[a]2[bc]"

But the thing is that even the 3 and 2 are strings at the moment- how can I loop through stringg while checking if each character is an integer?

for i in stringg: 
    if isinstance(i,int): 
        print("Hello")

The above loop returns nothing to my point made above. Any ideas how the above can be achieved?

One other thing that came to mind is to do int(i) as we loop, but I couldn't figure out how to do this.

  • strings in python has "isdigit" and "isnumeric" as functions you could use. – Christian Sloper Jun 21 '21 at 09:55
  • The accepted answer on the proposed duplicate will not work here. The OP cannot split by whitespace. The [2nd highest voted one](https://stackoverflow.com/a/4289348/6162307) does cover this case as well though. – Ma0 Jun 21 '21 at 09:58

1 Answers1

0
stringg = "3[a]2[bc]"

for ch in stringg:
    if ch.isnumeric():
        print(ch)
Meowster
  • 447
  • 3
  • 14