1
For example, for the program to tell the number of the hundreds of a number from:
número_int = int(input("Digite um número inteiro: "))
1
For example, for the program to tell the number of the hundreds of a number from:
número_int = int(input("Digite um número inteiro: "))
6
To take the digit of the hundred, just divide the value by 100, take only the whole part of the result and, if there are more digits, take the last of the sequence.
>>> numero_int = 5
>>> print(int(str(int(numero_int/100))[-1]))
0
>>> numero_int = 123
>>> print(int(str(int(numero_int/100))[-1]))
1
>>> numero_int = 14589
>>> print(int(str(int(numero_int/100))[-1]))
5
Split:
int(numero_int/100)
Takes the entire part of the result of dividing the number by 100.
str(...)[-1]
Converts the value to string and only looks for the last character. This is because if the number is greater than 1000, the entire part of the division by 100 will be greater than 10. For example: int(14589/100)
will be 14, what we want is 4.
int(...)
Converts the last character of the string for int again.
To take the entire part of the division, it is also possible to use the operator
//
, as innumero_int // 100
, being the equivalent ofint(numero_int / 100)
.
1
One solution is to divide integer by a power of 10, and use the rest of the split operation by 10. For example, in Python 2 to get the third digit from right to left:
>>> 9912345 / 100 % 10
3
In Python 3 a division between integers returns a float, so you need to use the integer split operator (it works in Python 2.7 as well):
>>> 9912345 // 100 % 10
3
0
Turn to String, then point to the location of the number.
num = int(input('Entre com um número de 0 a 9999: '))
n = str(num)
print('\nAnalizando o número {}'.format(n))
print('\nUnidade: {}'
'\nDezena: {}'
'\nCentena: {}'
'\nMilhar: {}'
.format(n[3], n[2], n[1], n[0])
)
0
num = 1894 unidade = num // 1 % 10 # 4 dezena = num // 10 % 10 # 9 centena = num // 100 % 10 # 8 milhar = num // 1000 % 10 # 1
-2
num = int(input('Enter Number: ')) u = in a // 1 % 10 d = num // 10 % 10 c = in // 100 % 10 m = num // 1000 % 10 print('Analyzing the Number {}'.format(num)) print('Unit {}:'' nDezena {}:'' nCentena {}:'' nMilhar: {}'. format(u, d, c, m))
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
If user type 123, the program must return 1?
– Woss
Exactly, but I have no idea how to do it
– Walkyrien