Python program to separate alphabets and numbers in a string
Given a string containing numbers and alphabets, the task is to write a Python program to separate alphabets and numbers from a string using regular expression.
Examples
Input: abcd11gdf15hnnn678hh4
Output: 11 15 678 4
abcd gdf hnnn hh
Explanation: The string is traversed from left to right and number and alphabets are separated from the given string .
Separate alphabets and numbers from a string using findall
In Python, we should import the regex library to use regular expressions. The pattern [0-9]+ is used to match the numbers in the string. Whereas ‘[a-zA-Z]’ is used to find all alphabets from the given string. We use the findall(Pattern, String) method that returns a list of all non-overlapping matches of the given pattern or regular expression in a string.
Python3
import re
def separateNumbersAlphabets( str ):
numbers = re.findall(r '[0-9]+' , str )
alphabets = re.findall(r '[a-zA-Z]+' , str )
print ( * numbers)
print ( * alphabets)
str = "adbv345hj43hvb42"
separateNumbersAlphabets( str )
|
Output:
345 43 42
adbv hj hvb
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(d + a), where n is the length of the input string, d is the number of digits, and a is the number of alphabets in the input string.
Filter alphabets and numbers from a string using split
The split(pattern, string) method splits the string together with the matched pattern, parses the string from left to right, and then produces a list of the strings that fall between the matched patterns.
Python3
import re
def separateNumbersAlphabets( str ):
numbers = []
alphabets = []
res = re.split( '(\d+)' , str )
for i in res:
if i > = '0' and i < = '9' :
numbers.append(i)
else :
alphabets.append(i)
print ( * numbers)
print ( * alphabets)
str = "geeks456for53geeks"
separateNumbersAlphabets( str )
|
Output:
456 53
geeks for geeks
Time complexity: O(n), where n is the length of the input string
Auxiliary space: O(n)
Separate alphabets and numbers using isalpha()
and isnumeric()
Step-by-step approach:
- Create two empty strings for storing the alphabets and numbers.
- Iterate through each character in the given string using a for loop.
- Check if the character is an alphabet or a number using the isalpha() and isnumeric() functions respectively.
- If the character is an alphabet, append it to the alphabets string. If it is a number, append it to the numbers string.
- Print the final alphabets and numbers strings.
Below is the implementation of the above approach:
Python3
def separateNumbersAlphabets( str ):
numbers = ''
alphabets = ''
for char in str :
if char.isalpha():
alphabets + = char
elif char.isnumeric():
numbers + = char
print (numbers)
print (alphabets)
str = "adbv345hj43hvb42"
separateNumbersAlphabets( str )
|
Time complexity: O(n), where n is the length of the given string.
Auxiliary space: O(n), for storing the alphabets and numbers strings.
Separate alphabets and numbers in a string Using List Comprehension
- Take the input string from the user.
- Use list comprehension to create two lists – one for alphabets and one for numbers, by iterating over each character of the input string and checking whether it is an alphabet or a digit.
- Join the alphabets and numbers lists using a space separator to obtain the final output string.
- Print the output string.
Python3
string = "abcd11gdf15hnnn678hh4"
alphabets_list = [char for char in string if char.isalpha()]
numbers_list = [char for char in string if char.isdigit()]
alphabets = ' ' .join(alphabets_list)
numbers = ' ' .join(numbers_list)
print ( "Alphabets and numbers in the string are:" , alphabets, numbers)
|
Output
Alphabets and numbers in the string are: a b c d g d f h n n n h h 1 1 1 5 6 7 8 4
Time Complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(n), where n is the length of the input string.
Last Updated :
27 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...