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 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
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 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
Python program to remove the nth index character from a non-empty string Given a String, the task is to write a Python program to remove the nth index character from a non-empty string Examples: Input: str = "Stable" Output: Modified string after removing 4 th character Stabe Input: str = "Arrow" Output: Modified string after removing 4 th character Arro The first approa
4 min read
Python - Splitting Text and Number in string Given a string containing both letters and numbers, the task is to separate the text (letters) and the numbers into two separate outputs. For example, if the input is "abc123", the output should be "abc" and "123". Let's discuss various ways in which we can achieve this in Python.Using for loopThis
3 min read
Sort given list of strings by part the numeric part of string - Python We are given a list of strings containing both letters and numbers, and the goal is to sort them based on the numeric part within each string. To do this, we extract the digits, convert them to integers and use them as sorting keys. For example, in ["Gfg34", "is67", "be3st"], the numbers 34, 67, and
3 min read
Python | Remove the given substring from end of string Sometimes we need to manipulate our string to remove extra information from the string for better understanding and faster processing. Given a task in which the substring needs to be removed from the end of the string using Python. Â Â Remove the substring from the end of the string using Slicing In
3 min read
Python Program to Converts Characters To Uppercase Around Numbers Given a String, the following program converts the alphabetic character around any digit to its uppercase. Input : test_str = 'geeks4geeks is best1 f6or ge8eks' Output : geekS4Geeks is besT1 F6Or gE8Ek Explanation : S and G are uppercased as surrounded by 4.Input : test_str = 'geeks4geeks best1 f6or
8 min read