-4

Say I have an arbitrary string like

"abc 123 def 456"

How would I take out the integers so that it can print "123456" without using regex?

4
  • 2
    print "123456" will do. Commented Nov 3, 2017 at 3:00
  • 1
    I dont think I worded it properly. I edited it so hopefully it makes more sense now. I need to extract the integers from any random string that could be given to me Commented Nov 3, 2017 at 3:03
  • Possible duplicate of remove numbers string python Commented Nov 3, 2017 at 3:05
  • You can use "".join(c for c in s if c.isdigit()), where s is your string. Commented Nov 3, 2017 at 3:06

1 Answer 1

0

for your specific question, you can just use .isdigit()

s = "abc 123 def 456"

for ch in s:
    if ch.isdigit():
        print(ch, end='')
print()
Sign up to request clarification or add additional context in comments.

5 Comments

please do not encourage zero effort questions
How is it zero effort? I think the first time I posted this it may have seemed like a dumb question, so I editied it.I need to extract the integers out of any string that is given to me, not just the specific one.
this will extract (print) integers from any given string
@Rionic I understand that very well and my "solution" was sarcastic, but you have shown no code, no search, no effort whatsoever to solve your problem.
This is just a part of my full question, but it's the part I can't figure out, so I decided to just leave out the rest of my code as it would be unecessary. I've been working on the question for quite a while now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.