How to find the numbers in the thousands, hundreds, tens, and ones place in PYTHON for an input number? For example: 256 has 6 ones, 5 tens, etc
Asked Answered
T

15

14
num = int(input("Please give me a number: "))
print(num)
thou = int((num // 1000))
print(thou)
hun = int((num // 100))
print(hun)
ten =int((num // 10))
print(ten)
one = int((num // 1))
print(one)

I tried this but it does not work and I'm stuck.

Tranship answered 24/9, 2015 at 3:18 Comment(4)
Hi, and welcome to Stack Overflow. Can you be more specific in your question? What do you mean, "it does not work"?Blinni
Please include the actual output and expected output.Doorman
@Robᵩ sure this don't work, because 120 // 10 is 12.Hedron
If you need an int to text conversion here is a gist in pythonBair
B
17

You might want to try something like following:

def get_pos_nums(num):
    pos_nums = []
    while num != 0:
        pos_nums.append(num % 10)
        num = num // 10
    return pos_nums

And call this method as following.

>>> get_pos_nums(9876)
[6, 7, 8, 9]

The 0th index will contain the units, 1st index will contain tens, 2nd index will contain hundreds and so on...

This function will fail with negative numbers. I leave the handling of negative numbers for you to figure out as an exercise.

Behling answered 28/6, 2018 at 0:28 Comment(0)
H
7

Like this?

a = str(input('Please give me a number: '))

for i in a[::-1]:
    print(i)

Demo:

Please give me a number: 1324
4
2
3
1

So the first number is ones, next is tens, etc.

Hedron answered 24/9, 2015 at 3:24 Comment(1)
This is a good hack. But interviewers don't want this.Frequency
L
4
num = 1234

thousands = num // 1000
hundreds = (num % 1000) // 100
tens = (num % 100) // 10
units = (num % 10)

print(thousands, hundreds, tens, units)
# expected output: 1 2 3 4

"//" in Python stands for integer division. It largely removes the fractional part from the floating-point number and returns an integer

For example:

4/3 = 1.333333
4//3 = 1
Laxation answered 8/6, 2021 at 2:19 Comment(0)
W
1

You could try splitting the number using this function:

def get_place_values(n):
    return [int(value) * 10**place for place, value in enumerate(str(n)[::-1])]

For example:

get_place_values(342)
>>> [2, 40, 300]

Next, you could write a helper function:

def get_place_val_to_word(n):
    n_str = str(n)
    num_to_word = {
        "0": "ones",
        "1": "tens",
        "2": "hundreds",
        "3": "thousands"
    }
    return f"{n_str[0]} {num_to_word[str(n_str.count('0'))]}"

Then you can combine the two like so:

def print_place_values(n):
    for value in get_place_values(n):
        print(get_place_val_to_word(value))

For example:

num = int(input("Please give me a number: "))
# User enters 342
print_place_values(num)
>>> 2 ones
4 tens
3 hundreds
Windcheater answered 29/6, 2021 at 2:19 Comment(0)
A
1
num=1234
digit_at_one_place=num%10
print(digit_at_one_place)
digits_at_tens_place=(num//10)%10
digits_at_hund_place=(num//100)%10
digits_at_thou_place=(num//1000)%10
print(digits_at_tens_place)
print(digits_at_hund_place)
print(digits_at_thou_place)

this does the job. it is simple to understand as well.

Ambulate answered 26/11, 2021 at 5:21 Comment(0)
A
0

Please note that I took inspiration from the above answer by 6pack kid to get this code. All I added was a way to get the exact place value instead of just getting the digits segregated.

num = int(input("Enter Number: "))
c = 1
pos_nums = []
while num != 0:
    z = num % 10
    pos_nums.append(z *c)
    num = num // 10
    c = c*10
print(pos_nums)

Once you run this code, for the input of 12345 this is what will be the output:

Enter Number: 12345
[5, 40, 300, 2000, 10000]

This helped me in getting an answer to what I needed.

Affirmative answered 12/8, 2019 at 10:10 Comment(0)
D
0
money = int(input("Enter amount: "))
thousand = int(money // 1000)
five_hundred = int(money % 1000 / 500)
two_hundred = int(money % 1000 % 500 / 200)
one_hundred = int(money % 1000 % 500 % 200 / 100)
fifty  = int(money % 1000 % 500 % 200 % 100 / 50)
twenty  = int(money % 1000 % 500 % 200 % 100 % 50 / 20)
ten  = int(money % 1000 % 500 % 200 % 100 % 50 % 20 / 10)
five  = int(money % 1000 % 500 % 200 % 100 % 50 % 20 % 10 / 5)
one = int(money % 1000 % 500 % 200 % 100 % 50 % 20 % 10 % 5 / 1)
if thousand >=1: 
  print ("P1000: " , thousand)
if five_hundred >= 1:
  print ("P500: " , five_hundred)
if two_hundred >= 1:
  print ("P200: " , two_hundred)
if one_hundred >= 1:
  print ("P100: " , one_hundred)
if fifty >= 1:
  print ("P50: " , fifty)
if twenty >= 1:
  print ("P20: " , twenty)
if ten >= 1:
  print ("P10: " , ten)
if five >= 1:
  print ("P5: " , five)
if one >= 1:
  print ("P1: " , one)
Drumstick answered 2/2, 2020 at 14:53 Comment(0)
E
0

Quickest way:

num = str(input("Please give me a number: "))
print([int(i) for i in num[::-1]])
Ephemerid answered 29/6, 2021 at 11:45 Comment(0)
Q
0

This will do it, doesn't use strings at all and handles any integer passed for col sensibly.

def tenscol(num: int, col: int):
    ndigits = 1
    while (num % (10**ndigits)) != num:
        ndigits += 1
    x = min(max(1, col), ndigits)
    y = 10**max(0, x - 1)
    return int(((num % 10**x) - (num % y)) / y)

usage:

print(tenscol(9785,-1))
print(tenscol(9785,1))
print(tenscol(9785,2))
print(tenscol(9785,3))
print(tenscol(9785,4))
print(tenscol(9785,99))

Output:

5
5
8
7
9
9
Quieten answered 6/11, 2021 at 21:56 Comment(0)
P
0
def get_pos(num,unit):
    return int(abs(num)/unit)%10

So for "ones" unit is 1 while for "tens", unit is 10 and so forth.

It can handle any digit and even negative numbers effectively.

So given the number 256, to get the digit in the tens position you do

get_pos(256,10)
>> 5
Parthenon answered 12/11, 2021 at 22:4 Comment(0)
P
0

I had to do this on many values of an array, and it's not always in base 10 (normal counting - your tens, hundreds, thousands, etc.). So the reference is slightly different: 1=1st place (1s), 2=2nd place (10s), 3=3rd place (100s), 4=4th place (1000s). So your vectorized solution:

import numpy as np
def get_place(array, place):
    return (array/10**(place-1)%10).astype(int)

Works fast and also works on arrays in different bases.

Procne answered 7/4, 2022 at 20:15 Comment(0)
O
0
# method 1

num = 1234
while num>0:
    print(num%10)
    num//=10

# method 2

num = 1234

print('Ones Place',num%10)
print('tens place',(num//10)%10)
print("hundred's place",(num//100)%10)
print("Thousand's place ",(num//1000)%10)
Operation answered 2/12, 2022 at 13:57 Comment(1)
Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.Genaro
H
0

Divide by place value for you:)

It gets an error in one of my programs.

OverflowError

pos_nums = []

def get_pos_nums(num):
    while num != 0:
        pos_nums.append(num % 10)
        num = num // 10
    return pos_nums

def return_divisible(number_to_divide_by_place):
    y = 0
    string = ""
    divisible = "0"

    get_pos_nums(9999)

    for x in pos_nums:
      y += 1
      if y > 1:
        string += "0"
      divisible = "1" + string
      divisible = int(divisible)
      divideddivisible = number_to_divide_by_place/divisible

    print(divideddivisible)
    return(divideddivisible)

return_divisible(9999)
Hearsh answered 19/7 at 18:22 Comment(0)
G
-1

In Python, you can try this method to print any position of a number.

For example, if you want to print the 10 the position of a number, Multiply the number position by 10, it will be 100, Take modulo of the input by 100 and then divide it by 10.

Note: If the position get increased then the number of zeros in modulo and in divide also increases:

input = 1234

print(int(input % 100) / 10 )

Output:

3
Groningen answered 26/4, 2021 at 16:50 Comment(1)
A code-styled, function-based program might be better: python def tens_of_number(number: int): return number % 100 / 10 num = 1234 print("tens of ", num, ":", tens_of_number(num)) Prophesy
C
-1

So I saw what another users answer was and I tried it out and it didn't quite work, Here's what I did to fix the problem. By the way I used this to find the tenth place of a number

# Getting an input from the user

input = int(input())

# Finding the tenth place of the number

print(int(input % 100) // 10)

Competition answered 28/6, 2021 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.