How to manipulate digits within an integer/string?
If you're starting with an integer, first convert it to a string; you can't address the digits within an integer conveniently:
>>> myint = 979
>>> mystr = str(myint)
>>> mystr
'979'
Address individual digits with their index in square brackets, starting from zero:
>>> mystr[1]
'7'
Convert those digits back to integers if you need to do math on them:
>>> int(mystr[1])
7
And if you're just doing a numerological summation, list comprehensions are convenient:
>>> sum( [ int(x) for x in mystr ] )
25
Just keep in mind that when you're considering individual digits, you're working with strings, and when you're doing arithmetic, you're working with integers, so this kind of thing requires a lot of conversion back and forth.
George Burrows
Updated on July 18, 2020Comments
-
George Burrows almost 4 years
I am looking for a general way to refer to particular digits in a integer or string, I need to be able to perform different operations on alternating digits and sum the result of all of those returned values.
Any help is much appreciated
Oh and I am a complete beginner so idiot-proof answers would be appreciated.
I will elaborate, is there any inbuilt function on Python that could reduce an integer into a list of it's digits, I have looked to no avail and I was hoping someone here would understand what I was asking, sorry to be so vague but I do not know enough of Python yet to provide a very in-depth question.
-
George Burrows over 12 yearsThank you very much, I did not know you could refer to items in something such as mystr in the same way you referred to items in a List, thanks for the basic explanation also. :)
-
jfs over 12 yearsyou could drop
[]
insidesum()
(use genexpr instead of listcomp). -
George Burrows over 12 yearsAlso is it possible to use the same start, stop, stride format in this instance? or is that reserved for lists? I.e. if I wanted to print the alternating digits from the end of an integer list1 = 0123456789, I could do print list1[-1::2] to return 8, 6, 4, 2, 0?
-
Russell Borogove over 12 years@GeorgeBurrows - lists, tuples, and strings are all sequences. The elements of a string sequence are strings of one character each. This is great when you want to pick characters out of a string, and confusing as heck when you accidentally pass a string to a function that expects a list.
-
Russell Borogove over 12 years
0123456789
without quotes is an integer, not a string, and the leading zero makes it octal, not decimal, so that's a problem. And your start-stop-stride numbers are wrong, but'0123456789'[-2::-2]
is'86420'
. Try starting a python shell (just typepython
at the command line) and experiment yourself. -
George Burrows over 12 yearsOk, I think I understand now, I will give it a go and see what happens, thanks to everyone that helped. :)
-
George Burrows over 12 yearsHow would you suggest applying this if I have the input in this format?: test_num('482898477382') ? As now I cannot simply refer to the string without typing the string in again and putting something along the lines of input = '482898477382'?
-
AlG over 12 yearsIf your input is a string you can omit the
str()
call and truncate it to[int(y) for y in x]
. I'll update my answer with an example. -
George Burrows over 12 yearsThis may be because I am new at this but to refer to mystr in such a manner would you have to explicitly put mystr = '47833893949' or could you refer to the string as mystr if it was defined as mystr('47833893949')? Thanks for the help :)
-
George Burrows over 12 yearsSorry I phrased my question a bit awkwardly, if defined the string as test('12345') could I then refer to the string '12345' simply as test? So then to turn the string '12345' into an integer I could simply put integer = int(test)?
-
AlG over 12 years
test('12345')
would try to call the functiontest()
with the parameter '12345'. To create a variable you'd dotest = '12345'
then, yes, you could doasInt = int(test)
-
retracile over 12 yearsThis assumes that
mystr
is the string representation of a number. If you have an integer,str(mynumber)
will give you the string representation. I don't know what you mean by "if it was defined as mystr('47833893949')"; that's not a variable definition, that's a function call. -
George Burrows over 12 yearsI have to define a function to check if a string input meets certain criteria, and the function has to be defined as: is_true(n) where n is the string, I need to change n into an integer, which from what I can gather would involve using for example integer = int(n) and I would have to type out n again, when the function should be able to have the number inputted just once and then it should be able to perform all the operations on n without needing any extra input. So ideally I would type n in the first time, and then it would give me an answer back of true or false if n meets these criteria.