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
# 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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
OutputInput 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)
OutputThe original string is : geek2eeks4g1eek5sbest6forall9
Strings after digits at end : geekeeksgeeksbestforall241569
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Python program to calculate the number of words and characters in the string
We are given a string we need to find the total number of words and total number of character in the given string.For Example we are given a string s = "Geeksforgeeks is best Computer Science Portal" we need to count the total words in the given string and the total characters in the given string. I
3 min read
Python program to count the number of spaces in string
In Python, there are various ways to Count the number of spaces in a String.Using count() Methodcount() method in Python is used to return the number of occurrences of a specified element in a list or stringPythons = "Count the spaces in this string." # Count spaces using the count() method space_co
3 min read
Python program to Increment Suffix Number in String
Given a String, the task is to write a Python program to increment the number which is at end of the string. Input : test_str = 'geeks006' Output : geeks7 Explanation : Suffix 006 incremented to 7. Input : test_str = 'geeks007' Output : geeks8 Explanation : Suffix 007 incremented to 8. Method #1 : U
5 min read
Python program to calculate the number of digits and letters in a string
In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list.Pythons = "Hello123!" # Initialize cou
3 min read
Check Whether String Contains Only Numbers or Not - Python
We are given a string s="12345" we need to check whether the string contains only number or not if the string contains only number we will return True or if the string does contains some other value then we will return False. This article will explore various techniques to check if a string contains
3 min read
Python program to extract numeric suffix from string
Given a string of characters with digits embedded in it. The task is to write a Python program to extract all the numbers that are trailing, i.e at the suffix of the string. Examples:Input : test_str = "GFG04"Output : 04Explanation : 04 is suffix of string and number hence extracted.Input : test_str
7 min read
Python Program to Reverse a Number
We are given a number and our task is to reverse its digits. For example, if the input is 12345 then the output should be 54321. In this article, we will explore various techniques for reversing a number in Python. Using String SlicingIn this example, the Python code reverses a given number by conve
3 min read
Python program to sort digits of a number in ascending order
Given an integer N, the task is to sort the digits in ascending order. Print the new number obtained after excluding leading zeroes. Examples: Input: N = 193202042Output: 1222349Explanation: Sorting all digits of the given number generates 001222349.Final number obtained after removal of leading 0s
2 min read
Python Program for Reverse of a Number Using Type Casting
Reversing a number is a common problem-solving exercise in programming, often used to teach the basics of algorithms and data manipulation. In Python, reversing a number can be efficiently achieved using type casting.Example:Input: 12345Output: 54321Input: -12345Output: -54321Input: 123.45Output: 54
4 min read
How to Append to String in Python ?
In Python, Strings are immutable datatypes. So, appending to a string is nothing but string concatenation which means adding a string at the end of another string.Let us explore how we can append to a String with a simple example in Python.Pythons = "Geeks" + "ForGeeks" print(s)OutputGeeksForGeeks N
2 min read