-3

Let's say x is an integer number. How can I print every even digit of this number?

For example, if the input is 34567910, then I want the output to be 4 6 9 0.

I know how to print every single digit using the following code, but I can't figure out how to print even digits only:

for i in str(x):   
    print(i)  
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ABSO
  • 17
  • 5
  • 1
    Duplicate: [How to slice a string to fetch characters at odd and even position](https://stackoverflow.com/questions/49563312/how-to-slice-a-string-to-fetch-characters-at-odd-and-even-position) – Tomerikoo Dec 02 '20 at 17:59
  • 2
    Welcome to SO! This site is not for receiving free-code. You are expected to do some research and ask about ***your own code***. Show a [mre] of your code and explain what is wrong: Are you getting an error, wrong output? Post example inputs/outputs. You can read more about [ask] and [How to ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – Tomerikoo Dec 02 '20 at 17:59

3 Answers3

1

Here is the solution to your problem. Note that I have to check if i is odd, due to Python indexes starting from 0. For example, in Python, an index of 1 is the second position.

num = input("Enter a number: ")

return_num = ""
# Iterates through input
for i in range(len(num)):
    # Checks if digit is at even position
    if i % 2 == 1:
        # If so, adds it to return_num
        return_num += num[i] # + " " if you want spaces between numbers

print(return_num) # Prints 4690 with your input

Alternatively, you could achieve this using one for-loop. (Credit to OneCricketeer.)

num = input("Enter a number: ")

return_num = ""
# Iterates through specified indexes of input
for i in range(1, len(num), 2): 
        return_num += num[i] # + " " if you want spaces between numbers

print(return_num) # Prints 4690 with your input

Or, if you want to have the shortest program humanly possible to solve your problem (Credit to Tomerikoo):

num = input("Enter a number: ")
print(num[1::2]) # Prints 4690 with your input
M-Chen-3
  • 2,036
  • 5
  • 13
  • 34
  • Why not use `range(1, len(num), 2)`? – OneCricketeer Dec 02 '20 at 17:49
  • I think you misunderstand my question ,if the input is 34567910 ,I want the output to be 4 6 9 0 this is what I meant by saying even orders in a number – ABSO Dec 02 '20 at 17:50
  • @OneCricketeer Thanks for your suggestion! The reason I think an if-statement is better is that it makes the program more customizable. I'll include your addition as well though. – M-Chen-3 Dec 02 '20 at 17:52
  • 1
    @M-Chen-3 ohh ,it does work. thank you – ABSO Dec 02 '20 at 17:55
0

This is one way how you can do it

# iterate over string
x = 34567910
string = str(x)
for index in range(len(string)):
    # check if index is divisible by 2
    if index % 2 != 0:
        # print character at index
        print(string[index], end = '')
Georgy
  • 12,464
  • 7
  • 65
  • 73
SkV
  • 60
  • 1
  • 1
  • 11
0
x = str(input())

Try using slicing and map method:

list(map(int, list(x)[1::2]))

[4, 6, 9, 0]
Pygirl
  • 12,969
  • 5
  • 30
  • 43
  • 1
    `str(input())` is redundant... `input` returns a string – Tomerikoo Dec 02 '20 at 18:10
  • By the way I see you voted to close the question. Then why answer? – Tomerikoo Dec 02 '20 at 18:11
  • Yes by default it takes that. I just put it there so that OP get this: x should be taken as str before passing it to list(x) – Pygirl Dec 02 '20 at 18:12
  • @Tomerikoo I answered the question by finding the even/odd digit(using filter) only then saw his comment on other answer that he was talking about index not the digit. So the question itself is not so clear that's why closed it. – Pygirl Dec 02 '20 at 18:17
  • 2
    Ok, I agree. I'm just saying it's a little contradictory to close a question and answer it... Closing means this question shouldn't accept any answers but yet you answered it... Either answer it or vote to close. Doing seems odd – Tomerikoo Dec 02 '20 at 18:19