0

Square Every Digit of a Number in Python? if we run 9119 through the function, 811181 will come out, because 92 is 81 and 12 is 1. write a code but this not working.

def sq(num):
words = num.split()  # split the text
for word in words:  # for each word in the line:
    print(word**2) # print the word

num = 9119
sq(num)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Abhiseck
  • 51
  • 1
  • 1
  • 8
  • Then you should fix it. If you want a more detailed answer, provide a more detailed question. (And fix you code's indentation.) – Scott Hunter Apr 02 '18 at 02:26
  • `split` can only be called on a string, not a number. Also, it splits strings into words by spaces; there are no spaces in `9119` even if you turn it into the string `'9119'`. – abarnert Apr 02 '18 at 02:30
  • If you want each character of your number, you can use: `for char in str(num)` (then you'll have to convert each back to a number and square them) – wolfson Apr 02 '18 at 02:31
  • If you want to loop over all of the digit in a number, there are two ways to do it: you can convert it to a string with `str` and then loop over that directly (don't call `split`—again, there are no spaces, and a string is already a sequence of characters), or use `//` and `%` (or `divmod`) to do it arithmetically. – abarnert Apr 02 '18 at 02:31
  • Also, `print(word**2)` prints a new line for each call by default. If you want to print things with no spaces between them, you need to `print(word**2, end='')`. – abarnert Apr 02 '18 at 02:33
  • it possible to `split` a number @abarnert – Abhiseck Apr 02 '18 at 02:50
  • 1
    No, it's not. Numbers do not have a `split` method; only strings (and bytes) do. – abarnert Apr 02 '18 at 03:06

6 Answers6

3

We can use list to split every character of a string, also we can use "end" in "print" to indicate the deliminter in the print out.

def sq(num):
    words = list(str(num)) # split the text
    for word in words:  # for each word in the line:
        print(int(word)**2, end="") # print the word

num = 9119
sq(num)

Alternatively

return ''.join(str(int(i)**2) for i in str(num))
Computing Corn
  • 117
  • 5
  • 13
Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31
2
def sq(num):
    z = ''.join(str(int(i)**2) for i in str(num))
    return int(z)
dsugasa
  • 663
  • 1
  • 9
  • 26
1
number=str(input("Enter the number :"))

def pc(number):

      digits = list(number)

      for j in digits:

      print(int(j)**2,end="")
 
pc(number)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Athithya
  • 11
  • 2
  • 4
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? That's always important, but it's _especially_ important here where there are four existing answers, two of which have been validated by the community. – Jeremy Caney Feb 12 '22 at 00:55
0

We can also support input of negative numbers and zeros. Uses arithmetic operators (% and //) for fun.

def sq(num):
    num = abs(num)                             #Handle negative numbers
    output = str((num % 10)**2)                #Process rightmost digit 

    while(num > 0):        
        num //= 10                             #Remove rightmost digit       
        output = str((num % 10)**2) + output   #Add squared digit to output
    print(output)
0

You can try this variant:

def square_digits(num):
    return int(''.join(str(int(i)**2) for i in str(num)))
ppwater
  • 2,315
  • 4
  • 15
  • 29
-1
def square_digits(num):
    num = str(num)
    result = ''
    for i in num:
        result += str(int(i)**2)
    return int(result)
var = square_digits(123)
print(var)
AMC
  • 2,642
  • 7
  • 13
  • 35