## 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
```

```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)

digit = [int(digi) for digi in list(dig) if digi.isdigit()]
return str(sum(digit) * 2)

```

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 : s = "123"

In : 2*sum([int(ss) for ss in s])
Out: 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'
```

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

```

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