Name not Found Name not Found - 1 year ago 98
Python Question

Choose a certain digit of a number

For example, in 3457, I would like to choose the third digit (5) and multiply it by three. How would I do this? I have tried changing digits to strings and then going from their however it didn't go well.

Answer Source

Let's say we have this integer:

num = 3457

To select specific numbers, let's first convert it to string.

num_str = str(num)

Now we can select the third character.

third_digit = num_str[2]

Before multiplying it, we have to convert it back to integer.

print(int(third_digit) * 3)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download