Python Program to move numbers to the end of the string
Last Updated :
20 Feb, 2023
Given a string, the task is to write a Python program to move all the numbers in it to its end.
Examples:
Input : test_str = ‘geek2eeks4g1eek5sbest6forall9’
Output : geekeeksgeeksbestforall241569
Explanation : All numbers are moved to end.
Input : test_str = ‘geekeeksg1eek5sbest6forall9’
Output : geekeeksgeeksbestforall1569
Explanation : All numbers are moved to end.
Method 1 : Using isdigit() and loop
In this, we check elements and digits using isdigit(), keeping track of all the numbers and append at end of string post iteration.
Python3
test_str = 'geek2eeks4g1eek5sbest6forall9'
print ( "The original string is : " + str (test_str))
res = ''
dig = ''
for ele in test_str:
if ele.isdigit():
dig + = ele
else :
res + = ele
res + = dig
print ( "Strings after digits at end : " + str (res))
|
Output
The original string is : geek2eeks4g1eek5sbest6forall9
Strings after digits at end : geekeeksgeeksbestforall241569
Time Complexity: O(n),, where n is the length of the input string, because it needs to iterate over every character in the string to check if it’s a digit or not and for adding the digits at the end.
Auxiliary Space: O(n), because it creates a new string to store the separated digits and the characters that aren’t digits, and it also creates a variable to store digits.
Method 2 : Using join()
In this, we perform the task of extracting digits and ignoring them using separate comprehensions and then joining both. At the end, digit string is joined at end of actual string.
Python3
test_str = 'geek2eeks4g1eek5sbest6forall9'
print ( "The original string is : " + str (test_str))
dig = ''.join(ele for ele in test_str if ele.isdigit())
res = ''.join(ele for ele in test_str if not ele.isdigit())
res + = dig
print ( "Strings after digits at end : " + str (res))
|
Output
The original string is : geek2eeks4g1eek5sbest6forall9
Strings after digits at end : geekeeksgeeksbestforall241569
Time Complexity: O(n), where n is the length of the string ‘test_str’.
Auxiliary Space: O(n), where n is the length of the string ‘test_str’.
Method 3: Without using any built-in method.
Python3
test_str = 'geek2eeks4g1eek5sbest6forall9'
print ( "The original string is : " + str (test_str))
digits = "0123456789"
res = ''
dig = ''
for ele in test_str:
if ele in digits:
dig + = ele
else :
res + = ele
res + = dig
print ( "Strings after digits at end : " + str (res))
|
Output
The original string is : geek2eeks4g1eek5sbest6forall9
Strings after digits at end : geekeeksgeeksbestforall241569
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using isnumeric() method
Python3
test_str = 'geek2eeks4g1eek5sbest6forall9'
print ( "The original string is : " + str (test_str))
res = ''
dig = ''
for ele in test_str:
if ele.isnumeric():
dig + = ele
else :
res + = ele
res + = dig
print ( "Strings after digits at end : " + str (res))
|
Output
The original string is : geek2eeks4g1eek5sbest6forall9
Strings after digits at end : geekeeksgeeksbestforall241569
Method 5 : Using isalpha() method
Python3
test_str = 'geek2eeks4g1eek5sbest6forall9'
print ( "The original string is : " + str (test_str))
res = ''
dig = ''
for ele in test_str:
if ele.isalpha():
res + = ele
else :
dig + = ele
res + = dig
print ( "Strings after digits at end : " + str (res))
|
Output
The original string is : geek2eeks4g1eek5sbest6forall9
Strings after digits at end : geekeeksgeeksbestforall241569
Time complexity: O(n), where n is the length of the input string test_str.
Auxiliary space: O(n), where n is the length of the input string test_str.
Method 6 : Using filter()+join()+list()+lambda functions
Python3
test_str = 'geek2eeks4g1eek5sbest6forall9'
print ( "The original string is : " + str (test_str))
dig = ''.join( list ( filter ( lambda x: x.isdigit(), test_str)))
res = ''.join( list ( filter ( lambda x: not x.isdigit(), test_str)))
res + = dig
print ( "Strings after digits at end : " + str (res))
|
Output
The original string is : geek2eeks4g1eek5sbest6forall9
Strings after digits at end : geekeeksgeeksbestforall241569
Time Complexity:O(N)
Auxiliary Space: O(N)
Method 7 : Using ord() method
Python3
test_str = 'geek2eeks4g1eek5sbest6forall9'
print ( "The original string is : " + str (test_str))
res = ""
dig = ""
for i in test_str:
if ord (i)> = 48 and ord (i)< = 57 :
dig + = i
else :
res + = i
res + = dig
print ( "Strings after digits at end : " + str (res))
|
Output
The original string is : geek2eeks4g1eek5sbest6forall9
Strings after digits at end : geekeeksgeeksbestforall241569
Time Complexity:O(N)
Auxiliary Space: O(N)
Method 8 : Using Regular expressions:
Python3
import re
inputString = 'geek2eeks4g1eek5sbest6forall9'
print ( "Input string: " , inputString)
digits = ' '.join(re.findall(' \d', inputString))
resultantString = re.sub( '\d' , '', inputString)
resultantString + = digits
print ( "Resultant string after adding digits at the end:\n" , resultantString)
|
Output
Input string: geek2eeks4g1eek5sbest6forall9
Resultant string after adding digits at the end:
geekeeksgeeksbestforall241569
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 9 : Using translate() function:
Python3
test_str = 'geek2eeks4g1eek5sbest6forall9'
print ( "The original string is : " + test_str)
import string
table = str .maketrans(' ', ' ', string.digits)
res = test_str.translate(table)
dig = ''.join([char for char in test_str if char.isdigit()])
res + = dig
print ( "Strings after digits at end : " + res)
|
Output
The original string is : geek2eeks4g1eek5sbest6forall9
Strings after digits at end : geekeeksgeeksbestforall241569
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...