Remove Numbers from a string python

Last Updated On Sunday 16th Jan 2022

Python String .isdigit() method.

  • Let’s Apply it on any string, and it’ll return a boolean of true or false.
	str = "1234";
print(str.isdigit()) # True
	

remove numbers from String

  • Now, we run with a string of numbers that will return true.
	str = "isStr123";
print(str.isdigit()) # False
	
  • If the given string input is a mix of numbers and letters, it’s going to return false.

.isdigit() needs to all numbers to work.

  • If the given String is decimals like, say, 0.1, that’s not going to work.
	str = "0.1234";

print(str.isdigit()) # False
	
	arr = "Hello1234"

print([i for i in arr if not i.isdigit()])

# ['H', 'e', 'l', 'l', 'o']
	
  • The Above Code eliminates all the numbers. Now let’s join together with the .join() method.

String. join() method to remove the numbers from the String.

Python Join() Syntax

	string_token.join(iterable)

Parameters:
string_token => It is also a string such as space'' or comma "," etc..
iterable => It could be a list of strings, characters, and numbers
	
	arr = "Hello1234"

print(''.join([i for i in arr if not i.isdigit()]))

# Hello
	

References

.join() method

Python .isdigit() method