How do you find the 3 digit number in python?

Python Program to get a number num and check whether num is three digit number or not?.

Sample Input 1:

1000

Sample Output 1:

Not a three digit number

Sample Input 2:

987

Sample Output 2:

Three digit number

Flow Chart Design

Program or Solution

				
			
					
num1=int[input["Enter your number:"]]
if[num1 < 1000 and num1 > 99]:
    print["{} is a 3 digit number ".format[num1]]
else:
    print["{} is not a 3 digit number".format[num1]]


			
				
			

Program Explanation

Get input num from user using input[] method check whether the num is greater than 99 and less than 100 using if statement.

if it is true, then print num is three digit number using print[] method.

Else print num is not three digit number using print[] method.

I have a long text line with a lot of random words and numbers, i wish to assign a variable to the only 3 digit number in the line.

The number changes every different line but it is always only 3 digits. How does one search for the only 3 digit number in a linepython? There may be some 3 letter words so it must just be the number.

09824747 18 n 02 archer 0 bowman 0 003 @ 09640897 n 0000

in this example i want the variable digits = 003

asked Jun 12, 2013 at 13:54

JohnConneelyJohnConneely

1,3334 gold badges16 silver badges29 bronze badges

A regular expression with \b word boundaries would do the trick:

re.findall[r'\b\d{3}\b', inputtext]

returns a list of all 3-digit numbers.

Demo:

>>> import re
>>> inputtext = '09824747 18 n 02 archer 0 bowman 0 003 @ 09640897 n 0000'
>>> re.findall[r'\b\d{3}\b', inputtext]
['003']
>>> inputtext = 'exact: 444, short: 12, long: 1234, at the end of the line: 456'
>>> re.findall[r'\b\d{3}\b', inputtext]
['444', '456']

answered Jun 12, 2013 at 13:58

Martijn PietersMartijn Pieters

986k274 gold badges3881 silver badges3238 bronze badges

You can use regular expressions. Or look for a digit, then check the next two characters manually.

I would use a regexp:

import re

threedig = re.compile[r'\b[\d{3}]\b'] # Regular expression matching three digits.

The \b means "word boundary", and [\d{3}] means "three digits", the parenthesis makes it a "group" so the matching text can be found.

Then search using:

mo = threedig.search["09824747 18 n 02 archer 0 bowman 0 003 @ 09640897 n 0000"]
if mo:
  print mo.group[1]

The above prints 333.

answered Jun 12, 2013 at 13:56

2

A solution thanks to regular expressions:

>>> s = "007 09824747 18 n 02 archer 0 bowman 0 003 @ 09640897 n 0000 008"
>>> r = re.findall[r'[?:[^\d]|\A][\d{3}][?:[^\d]|\Z]', s]
>>> r
['007', '003', '008']

answered Jun 12, 2013 at 14:01

EmmanuelEmmanuel

13.5k11 gold badges48 silver badges72 bronze badges

In Python, I got the following to work [based on the answers above]:

re.compile['prefix\d{1,3}\suffix']}]

This covers the scenario for anywhere between 1-3 digits

answered Apr 30, 2018 at 17:15

raTMraTM

3691 gold badge3 silver badges16 bronze badges

How do you make a 3 digit number in Python?

If you need to generate a random 3-digit number, use the randint[] function..
Use the str[] class to convert the number to a string..
Use the str. zfill[] method to format the number to 3 digits..
The str. zfill[] method will format the number to 3 digits by left-filling it with 0 digits..

How do I find the 3 digit number in a string Python?

I would use a regexp: import re threedig = re. compile[r'\b[\d{3}]\b'] # Regular expression matching three digits. The \b means "word boundary", and [\d{3}] means "three digits", the parenthesis makes it a "group" so the matching text can be found.

How do you find the digits of a number in Python?

int[str[number][i-1]] . Or if you need to handle all digits: for index, digit in enumerate[str[number], start=1]: digit = int[digit] ... .
Stack Overflow is not a code-writing or tutorial service..

How do you reverse a 3 digit number in Python?

Algorithm.
Input Integer: number..
[1] Initialize variable revs_number = 0..
[2] Loop while number > 0..
[a] Multiply revs_number by 10 and add remainder of number..
divide by 10 to revs_number..
revs_number = revs_number*10 + number%10;.
[b] Divide num by 10..
[3] Return revs_number..

Chủ Đề