Open In App

Python Program to move numbers to the end of the string

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# initializing string
test_str = 'geek2eeks4g1eek5sbest6forall9'
 
# printing original string
print("The original string is : " + str(test_str))
 
# getting all numbers and removing digits
res = ''
dig = ''
for ele in test_str:
    if ele.isdigit():
        dig += ele
    else:
        res += ele
 
# adding digits at end
res += dig
 
# printing result
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




# initializing string
test_str = 'geek2eeks4g1eek5sbest6forall9'
 
# printing original string
print("The original string is : " + str(test_str))
 
# getting all numbers
dig = ''.join(ele for ele in test_str if ele.isdigit())
 
# getting all elements not digit
res = ''.join(ele for ele in test_str if not ele.isdigit())
 
# adding digits at end
res += dig
 
# printing result
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




# initializing string
test_str = 'geek2eeks4g1eek5sbest6forall9'
 
# printing original string
print("The original string is : " + str(test_str))
 
digits="0123456789"
 
# getting all numbers and removing digits
res = ''
dig = ''
for ele in test_str:
    if ele in digits:
        dig += ele
    else:
        res += ele
 
# adding digits at end
res += dig
 
# printing result
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




# initializing string
test_str = 'geek2eeks4g1eek5sbest6forall9'
 
# printing original string
print("The original string is : " + str(test_str))
 
# removing digits
res = ''
dig = ''
for ele in test_str:
    if ele.isnumeric():
        dig += ele
    else:
        res += ele
 
# adding digits at end
res += dig
 
# printing result
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




#Python Program to move numbers to the end of the string
# initializing string
test_str = 'geek2eeks4g1eek5sbest6forall9'
 
# printing original string
print("The original string is : " + str(test_str))
 
# removing digits
res = ''
dig = ''
for ele in test_str:
    if ele.isalpha():
        res += ele
    else:
        dig += ele
 
# adding digits at end
res += dig
 
# printing result
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




# initializing string
test_str = 'geek2eeks4g1eek5sbest6forall9'
 
# printing original string
print("The original string is : " + str(test_str))
 
# getting all numbers
dig = ''.join(list(filter(lambda x: x.isdigit(), test_str)))
 
# getting all elements not digit
res = ''.join(list(filter(lambda x: not x.isdigit(), test_str)))
 
# adding digits at end
res += dig
 
# printing result
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




#Python Program to move numbers to the end of the string
# initializing string
test_str = 'geek2eeks4g1eek5sbest6forall9'
 
# printing original string
print("The original string is : " + str(test_str))
 
# removing digits
res=""
dig=""
for i in test_str:
    if ord(i)>=48 and ord(i)<=57 :
        dig+=i
    else:
        res+=i
res+=dig
# printing result
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'
# printing the input string
print("Input string: ", inputString)
# Extracting digits using regular expressions
digits = ''.join(re.findall('\d', inputString))
# Removing digits using regular expressions
resultantString = re.sub('\d', '', inputString)
# Concatenating/adding digits at the end of the resultant string
resultantString += digits
print("Resultant string after adding digits at the end:\n", resultantString)
#This code is contributed by Jyothi pinjala


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




# initializing string
test_str = 'geek2eeks4g1eek5sbest6forall9'
 
# printing original string
print("The original string is : " + test_str)
 
# getting all numbers and removing digits
import string
 
table = str.maketrans('', '', string.digits)
res = test_str.translate(table)
dig = ''.join([char for char in test_str if char.isdigit()])
 
# adding digits at end
res += dig
 
# printing result
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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads