Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to move numbers to the end of the string
In this article, we will learn different methods to move all numeric digits from their current positions to the end of a string while preserving the order of non-digit characters.
Problem Overview
Given a string containing letters, digits, and spaces, we need to extract all digits and append them to the end while keeping other characters in their original order.
Input
inputString = 'hello5691 tutorialspoint1342'
Output
Resultant string after adding digits at the end: hello tutorialspoint56911342
Here all the numbers 56911342 contained in the input string are moved to the end of the string.
Method 1: Using isdigit() and for loop
This approach iterates through each character, separating digits from non-digits into different strings, then concatenates them ?
# input string
inputString = 'hello5691 tutorialspoint1342'
# printing the input string
print("Input string:", inputString)
# storing resultant string
resultantString = ''
# storing all digits
digits = ''
# traversing through each character of the input string
for c in inputString:
# checking whether the current character is a digit or not
if c.isdigit():
# add that character to a digits string, if the condition is true
digits += c
else:
# else add that character to the above resultant string
resultantString += c
# concatenating/adding digits at the end of the resultant string
resultantString += digits
# printing resultant string after adding digits at the end
print("Resultant string after adding digits at the end:")
print(resultantString)
Input string: hello5691 tutorialspoint1342 Resultant string after adding digits at the end: hello tutorialspoint56911342
Method 2: Using isdigit() and join() functions
The join() function connects sequence elements using list comprehension for a more concise solution ?
# input string
inputString = 'hello5691 tutorialspoint1342'
# printing the input string
print("Input string:", inputString)
# traversing through each character of a string and getting all digits
digits = ''.join(c for c in inputString if c.isdigit())
# getting all other characters(not digits)
resultantString = ''.join(c for c in inputString if not c.isdigit())
# concatenating/adding digits at the end of the resultant string
resultantString += digits
print("Resultant string after adding digits at the end:")
print(resultantString)
Input string: hello5691 tutorialspoint1342 Resultant string after adding digits at the end: hello tutorialspoint56911342
Method 3: Without using any built-in functions
This method manually checks if each character exists in a predefined digits string ?
# input string
inputString = 'hello5691 tutorialspoint1342'
# printing the input string
print("Input string:", inputString)
# storing all digits
digits_str = "0123456789"
# storing all characters except digits
resultantString = ''
# storing all digits of an input string
resultDigits = ''
# traversing through each character of a string
for c in inputString:
# checking whether that current character is in the above digits string
if c in digits_str:
# adding that character to the resultDigits string
resultDigits += c
else:
# else adding that character to the resultant string
resultantString += c
# concatenating/adding digits at the end of the resultant string
resultantString += resultDigits
print("Resultant string after adding digits at the end:")
print(resultantString)
Input string: hello5691 tutorialspoint1342 Resultant string after adding digits at the end: hello tutorialspoint56911342
Method 4: Using isnumeric() function
The isnumeric() function returns True if all characters in a string are numeric (0-9), similar to isdigit() ?
# input string
inputString = 'hello5691 tutorialspoint1342'
# printing the input string
print("Input string:", inputString)
# storing all characters except digits
resultantString = ''
# storing all digits
digits = ''
# traversing through each character of an input string
for c in inputString:
# checking whether the current character is a digit
if c.isnumeric():
# adding that digit to the above digits string
digits += c
else:
# else adding that character to the resultantString
resultantString += c
# concatenating/adding digits at the end of the resultant string
resultantString += digits
print("Resultant string after adding digits at the end:")
print(resultantString)
Input string: hello5691 tutorialspoint1342 Resultant string after adding digits at the end: hello tutorialspoint56911342
Method 5: Using isalpha() function
The isalpha() function returns True if all characters are alphabets (a-z), so we use reverse logic to identify digits ?
# input string
inputString = 'hello5691 tutorialspoint1342'
# printing the input string
print("Input string:", inputString)
# storing all characters except digits
resultantString = ''
# storing all digits
digits = ''
# traversing through each character of a string
for c in inputString:
# checking whether current character is an alphabet or space
if c.isalpha() or c == ' ':
# adding that character to the resultantString
resultantString += c
else:
# else adding that digit to the digits
digits += c
# concatenating/adding digits at the end of the resultant string
resultantString += digits
print("Resultant string after adding digits at the end:")
print(resultantString)
Input string: hello5691 tutorialspoint1342 Resultant string after adding digits at the end: hello tutorialspoint56911342
Comparison
| Method | Function Used | Readability | Performance |
|---|---|---|---|
| Method 1 | isdigit() |
Good | Standard |
| Method 2 |
join() + isdigit()
|
Excellent | Best |
| Method 3 | Manual checking | Fair | Slower |
| Method 4 | isnumeric() |
Good | Standard |
| Method 5 | isalpha() |
Good | Standard |
Conclusion
Method 2 using join() with list comprehension provides the most concise and efficient solution. All methods successfully move digits to the end while preserving the original order of characters.
