“Como extrair números de uma string no python?” Respostas de código

Extraia números de string python

# Python program to extract digits from string

# take string
string = "kn4ow5pro8am2"

# print original string
print("The original string:", string)

# using join() + filter() + isdigit()
num = ''.join(filter(lambda i: i.isdigit(), string))

# print extract digits
print("Extract Digits:", num)
Mighty Unicorn

Extrair Python todos os números da string re

>>> str = "h3110 23 cat 444.4 rabbit 11 2 dog"
>>> [int(s) for s in str.split() if s.isdigit()]
[23, 11, 2]
Comfortable Cardinal

Como extrair números inteiros do String Python

>>> txt = "h3110 23 cat 444.4 rabbit 11 2 dog"
>>> [int(s) for s in txt.split() if s.isdigit()]
[23, 11, 2]
Alert Armadillo

Como extrair dígitos de uma string em python

# Get digit using regex
import re

def extract_num_from_string(string: str) -> int:
  return ''.join(re.findall('\d', string))

print(extract_num_from_string('Year 2000'))

# output will be: 2000
Wild Wombat

Respostas semelhantes a “Como extrair números de uma string no python?”

Perguntas semelhantes a “Como extrair números de uma string no python?”

Mais respostas relacionadas para “Como extrair números de uma string no python?” em Python

Procure respostas de código populares por idioma

Procurar outros idiomas de código