 # How to sum the digits in a string in Python

How to sum the digits in a string in Python. That’s the topic I want to introduce to you today. I would sum the digits on a string containing digits and non-digits, and the string consists of only digits. If you’re curious about this topic, read it all.

## Sum the digits in a string in Python

### String containing digits and non-digits.

#### Use the str.isdigit() function.

Syntax:

str.isdigit()

The str.isdigit() function returns True if the string contains only digits and False otherwise.

Example:

• Declare a function that sums the digits in a string.
• The for loop loops through that string. In each iteration, look up the digit using the str.isdigit() function. If it’s a digit, use the int() function to convert it back to an integer.
• Finally, sum those integers.
def sumDigitsStr(strObj):
sumDigit = 0
for i in strObj:
if i.isdigit() == True:
digits = int(i)
sumDigit = sumDigit + digits
return sumDigit

print('The sum of the digits in the string is:', sumDigitsStr('12learnshareit34'))

Output:

The sum of the digits in the string is: 10

With the same idea, you can use list comprehension to make everything faster.

Example:

testStr = '12learnshareit34'

# Use the list comprehension to the sum of digits
sumDigits = sum(int(i) for i in testStr if i.isdigit())
print('The sum of the digits in the string is:', sumDigits)

Output:

The sum of the digits in the string is: 10

#### Use the str.isnumeric() function.

The function str.isnumeric() and the function str.isdigit() are pretty similar in effect, so the idea of ​​using the str.isnumeric() function is similar to using the str.isdigit() function.

Syntax:

str.isnumeric()

Example:

• Declare a function that sums the digits in a string.
• The for loop loops through that string. In each iteration, look up the digit using the str.isnumeric() function. If it’s a digit, use the int() function to convert it back to an integer.
• Finally, sum those integers.
def sumDigitsStr(strObj):
sumDigit = 0
for i in strObj:
if i.isnumeric() == True:
digits = int(i)
sumDigit = sumDigit + digits
return sumDigit

print('The sum of the digits in the string is:', sumDigitsStr('12learnshareit34'))

Output:

The sum of the digits in the string is: 10

You can also use list comprehension to do the sum of digits in this way.

Example:

testStr = '12learnshareit34'

# Use the list comprehension to the sum of digits
sumDigits = sum(int(i) for i in testStr if i.isnumeric())
print('The sum of the digits in the string is:', sumDigits)

Output:

The sum of the digits in the string is: 10

### The string consists of only digits.

For strings containing only digits, you won’t have to use the str.isdigit() function and the str.numeric() function to check.

Example:

testStr = '1234'

# Use the list comprehension to the sum of digits
sumDigits = sum(int(i) for i in testStr)
print('The sum of the digits in the string is:', sumDigits)

Output:

The sum of the digits in the string is: 10

## Summary:

In conclude, this article have shown you the causes with several ways to sum the digits in a string in Python. If you have any suggestions or comments about the article, please comment.