1

I know isdigit returns true or false if the complete string is a number but I wanna check if the string contains a number like for example "1dfsfsfs" would return false but it does contain a digit. So which method would I use to find this out ?

.isdigit()
Angel Valenzuela
  • 377
  • 1
  • 5
  • 15
  • Check each character in the string in turn. See also e.g. the `any()` function, which makes tasks like that easier. – Ulrich Eckhardt Jun 15 '18 at 05:16
  • isdigit() will always return false if your string has more than just digits. Why not use the str.count function? – cs95 Jun 15 '18 at 05:16
  • 1
    If you just need to return a boolean you can use regex to match any digit in the string `\d` – Adriano Jun 15 '18 at 05:16

1 Answers1

3

try this with map and lambda

a= "a12345"
sum(list(map(lambda x:1 if x.isdigit() else 0,set(a))))

it will give you count of digit in string

explanation: set(a) --convert string into unique item (because map function take an iterator or list for mapping it to a function )

lambda x:1 if x.isdigit() else 0

this lambda function help us to find if it digit then it will return 1 and if not it will return 0

list(map(lambda x:1 if x.isdigit() else 0,set(a)))

this will return somthing like this [0,1,1,1,1,1]

sum([0,1,1,1,1,1])

this will sum all the value in list and in this way you find the count

Pawanvir singh
  • 373
  • 6
  • 17