Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8496669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:55:03+00:00 2026-06-10T23:55:03+00:00

Assume I have a string as follows: expression = ‘123 + 321’ . I

  • 0

Assume I have a string as follows: expression = '123 + 321'.

I am walking over the string character-by-character as follows: for p in expression. I am I am checking if p is a digit using p.isdigit(). If p is a digit, I’d like to grab the whole number (so grab 123 and 321, not just p which initially would be 1).

How can I do that in Python?

In C (coming from a C background), the equivalent would be:

int x = 0;
sscanf(p, "%d", &x);
// the full number is now in x

EDIT:

Basically, I am accepting a mathematical expression from a user that accepts positive integers, +,-,*,/ as well as brackets: ‘(‘ and ‘)’. I am walking the string character by character and I need to be able to determine whether the character is a digit or not. Using isdigit(), I can that. If it is a digit however, I need to grab the whole number. How can that be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:55:05+00:00Added an answer on June 10, 2026 at 11:55 pm

    The Python documentation includes a section on simulating scanf, which gives you some idea of how you can use regular expressions to simulate the behavior of scanf (or sscanf, it’s all the same in Python). In particular, r'\-?\d+' is the Python string that corresponds to the regular expression for an integer. (r'\d+' for a nonnegative integer.) So you could embed this in your loop as

    integer = re.compile(r'\-?\d+')
    for p in expression:
        if p.isdigit():
            # somehow find the current position in the string
            integer.match(expression, curpos)
    

    But that still reflects a very C-like way of thinking. In Python, your iterator variable p is really just an individual character that has actually been pulled out of the original string and is standing on its own. So in the loop, you don’t naturally have access to the current position within the string, and trying to calculate it is going to be less than optimal.

    What I’d suggest instead is using Python’s built in regexp matching iteration method:

    integer = re.compile(r'\-?\d+') # only do this once in your program
    
    all_the_numbers = integer.findall(expression)
    

    and now all_the_numbers is a list of string representations of all the integers in the expression. If you wanted to actually convert them to integers, then you could do this instead of the last line:

    all_the_numbers = [int(s) for s in integer.finditer(expression)]
    

    Here I’ve used finditer instead of findall because you don’t have to make a list of all the strings before iterating over them again to convert them to integers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume we have a string like the following : ,34,23,4,5,634,23,12,5,4,3,1234,23,54,,,,,,,123,43,2,3,4,5,3424,,,,,,,,123,,,1234,,,,,,,45,,,56 How can we convert
assume I have a string 1,2,3,4 Now I want to replace, e.g. the 3rd
Let's assume I have the string NSString* myString = @Hello,; How can I remove
Assume I have the following string: Hellotoevryone<img height=115 width=150 alt= src=/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg />Iamsogladtoseeall. This
Assume I have a class that looks like this: class Sample { public string
Assume I have a set of numbers like 1,2,3,4,5,6,7 input as a single String
Assuming I have a numerical string: var foo = 0; Assume I want to
I have to parse a String that can assume hex values or other non-hex
I have a list of words (assume they are stored in String[] if you
I have a Map[Long, String] which I would like iterate over in descending order

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.