473,395 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

adding numbers together in a string

Thekid
145 100+
I know python has math functions like 5+6 and 5*6 and 5/6 etc...but I have something like this:
x=575124357

and need to add them all together:
5+7+5+1+2+4+3+5+8=40
Feb 5 '09 #1
2 3439
bvdet
2,851 Expert Mod 2GB
Following are two ways:
Expand|Select|Wrap|Line Numbers
  1. >>> x=575124357
  2. >>> reduce(lambda x, y: x+y, [int(s) for s in str(x)], 0)
  3. 39
  4. >>> sum([int(s) for s in str(x)])
  5. 39
  6. >>> 
Feb 5 '09 #2
Thekid
145 100+
Thanks again...! I like the simpler look of the sum([int]) line so I think I'll use that.
Feb 5 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Stuart | last post by:
I am occasionally having problems with adding numbers together. Sometimes (not always) 2 + 2 = 22, Obviousy this is a string issue. I am using the following method to overcome this problem, however...
10
by: sp0 | last post by:
Is there a reason why to make mix numbers improper when adding? It seems when subtracting and adding, adding a subtracting the whole numbers and fraction parts should be sufficient? what'ch think
15
by: crjunk | last post by:
I have 4 TextBoxes on my form that I'm trying to add together to get a grand total. Here is the code I'm using: <SCRIPT LANGUAGE="JavaScript"> <!-- Beginning of JavaScript - function...
15
by: Stormkid | last post by:
Hey Gang, I'm trying to figure out the best way to add two times together of the format hh:mm:ss any suggestions would be great thanks Todd
3
by: dave | last post by:
I'm trying to learn this. My problem, I have 2 strings that represent floats. I want to add them together. Should I look at using Cint to convert each character in the string and add...
6
by: Hareth | last post by:
Scenerio: label1.text = 0 textbox1.text = 0 textbox2.text = textbox1.text + label1.text It turns out it isnt adding the numbers, it attaches the new number next to the old number
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
4
by: begoddendave | last post by:
Hi I am trying to add 2 variables together but I think the computer see's the numbers as a string instead of an integer. I have looked for help with this but all I can see is ASP doesn't allow...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.