## adding numbers in a string and multiplying by 2 in python

I am trying to add the digits from a string and multiply it by 2.

ex) "123" would be (1+2+3)*2

I have tried this but I can't seem to get it working.

def add_digits_in_string(dig): initial = 0 strinitial = str(initial) for i in dig: t = str(i) if t.isdigit(): strinitial += int(t) strinitial*2 return strinitial

your code

def adddigitsinstring(dig): # dig is string "123" initial = 0 # initial is of type int strinitial = str(initial) # converting initial to string type into new varibale strinitial for i in dig: # looping through each digit in the dig t = str(i) # converting existing string to string , not required if t.isdigit(): # check if digit char is digit strinitial += int(t) # adding the int type with the str type wrong "2 +"2" == error strinitial*2 # multiple value with the 2, so in every loop it got multiple each time , not required return strinitial # return int value as str type ie "123"

this is what you should do, you need to keep checking the if each character in the input is a digit or not. if it is a digit then you need to keep adding the digits to a intial value of type int beacuse 1+1=2 and '1'+'1' ='11', once you add all the digits then multiply them by 2 and return the result in str format

def adddigitsinstring(dig:str): intial = 0 for digit in dig: if digit.isdigit(): intial += int(digit) final_result = intial * 2 return str(final_result) def add_digits_in_string(dig:str): digit = [int(digi) for digi in list(dig) if digi.isdigit()] return str(sum(digit) * 2) print(adddigitsinstring("12345")) print(add_digits_in_string("12345"))

output

30 30

In this program, we asked the user to enter two numbers and this program displays the sum of two numbers entered by user. We use the built-in function input () to take the input. Since, input () returns a string, we convert the string into number using the float () function. Then, the numbers are added.

You can use built-in function `sum`

, list expansion and `int`

to convert a digit to a string. You can iterate over consecutive characters easily.

In [8]: s = "123" In [9]: 2*sum([int(ss) for ss in s]) Out[9]: 12

In fact, you can use Python to multiply strings, which is actually pretty cool when you think about it. You can take a string and double, triple, even quadruple it with only a little bit of Python. There are a few different ways that we can go about multiplying strings, depending on how you want your multiplied strings to be formatted.

Try this using `list comprehension`

:

num = '123' print(sum(int(x) for x in num)*2)

and here you are if you really need that in a function:

def adddigitsinstring(dig): return sum(int(x) for x in dig)*2 num = '123' print(adddigitsinstring(num))

Output 4: Multiplication. Enter First Number: 6 Enter Second Number: 8 Enter which operation would you like to perform? Enter any of these char for specific operation +,-,*,/: * 6 * 8 : 48 Related Python Examples: 1. Python program to add two matrices 2. Python program to add two binary numbers 3. Python program to add two numbers 4.

You can sum a list comprehension the return the result multiplied by 2

def adddigitsinstring(dig): return sum([int(i) for i in dig if i.isdigit()]) * 2 print(adddigitsinstring("123"))

Find out the multiplication of two numbers in Python : Calculating the multiplication is a basic arithmetic operation. Almost in all programming language, the multiplication process is the same. In this tutorial, we will learn how to find out the multiplication of two numbers in python.

The most simple I could come up with:

def adddigitsinstring(dig): return 2*sum(map(lambda x:int(x), dig))

Sample tests:

>>> adddigitsinstring("1234") 20 >>> adddigitsinstring("9") 18

How can you add all numbers in a list in Python. Let us now see how to add all numbers in a list using a built-in function. We use sum() which is a built-in function in Python programming language. sum() The sum() is a built-in function as said earlier. This function will work only when numeric values are given to it, this will not work when

Understanding how to multiply and divide numbers in Python is important, not because you need to know the answer to something like 2 times 2, but because you can use these operations in your more complex code to achieve other functionalities. To multiply two numbers by each other, you'll use the "*" operator, like this: 2*4 5*10 3*7

Python - how to multiply characters in string by number after character an empty list where you add text and then glue it together with join, as your solution

Python – Multiply two list There can be many situations in which one requires to find index wise product of two different lists. This can have a possible applications in day-day programming.