-4

Is this possible? To delete everything in a string after any number?


Eg.:

some_text = "123 Text"
some_text_2 = "456 Foo"

I want to get only 123 and 456 without the space, and those numbers would be a random number

Diego Barreiro
  • 333
  • 8
  • 22

1 Answers1

1

With little search on Stack Overflow.

Removing numbers from string

For Python 2:

from string import digits

s = 'abc123def456ghi789zero0'
res = s.translate(None, digits)
# 'abcdefghizero'

For Python 3:

from string import digits

s = 'abc123def456ghi789zero0'
remove_digits = str.maketrans('', '', digits)
res = s.translate(remove_digits)
# 'abcdefghizero'

And add a replace function to remove the space.

Community
  • 1
  • 1
callmemath
  • 8,185
  • 4
  • 37
  • 50