
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to move numbers to the end of the string
In this article, we will learn a python program to move numbers to the end of the string.
Methods Used
The following are the various methods to accomplish this task −
Using isdigit() and for loop
Using isdigit() and join() functions
Without using any built-in functions
Using isnumeric() function
Using isalpha() function
Example
Assume we have taken an input string. We will now move all the numbers contained in an input string to the end of the string using the above methods.
Input
inputString = 'hello5691 tutorialspoint1342'
Output
Resultant string after adding digits at the end: hello tutorialspoint56911342
Here all the numbers 56911342 contained in an input string are moved to the end of the string.
Method 1: Using isdigit() and for loop
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task –.
Create a variable to store the input string.
Create an empty string to store the resultant string.
Create another empty string to store all digits.
Use the for loop to traverse through each character of an input string.
Apply isdigit() function to the current character to check whether the current character is a digit or not using an if conditional statement.
Add that current character to a digits string, if the condition is true.
Else add that character to the above resultant string.
Use the + operator(string concatenation) to add digits at the end of the resultant string.
Print the resultant string after adding digits at the end of the string.
Example
The following program returns a string after adding all the digits to the end of an input string using for loop and isdigit() function –
# 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:\n", resultantString)
Output
On executing, the above program will generate the following output –
Input string: hello5691 tutorialspoint1342 Resultant string after adding digits at the end: hello tutorialspoint56911342
Method 2: Using isdigit() and join() functions
join() function − join() is a string function in Python that is used to join elements of a sequence that are separated by a string separator. This function connects sequence elements to convert to a string.
Example
The following program returns a string after adding all the digits to the end of an input string using isdigit() and join() functions –
# 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:\n", resultantString)
Output
Input string: hello5691 tutorialspoint1342 Resultant string after adding digits at the end: hello tutorialspoint56911342
Method 3: Without using any built-in functions.
Example
The following program returns a string after adding all the digits to the end of an input string Without using any built-in functions –
# 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:\n", resultantString)
Output
Input string: hello5691 tutorialspoint1342 Resultant string after adding digits at the end: hello tutorialspoint56911342
Method 4: Using isnumeric() function
isnumeric() function − Returns True if all the characters in a string are numeric (0-9), else returns False.
Example
The following program returns a string after adding all the digits to the end of an input string using isnumeric() function –
# 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:\n", resultantString)
Output
Input string: hello5691 tutorialspoint1342 Resultant string after adding digits at the end: hello tutorialspoint56911342
Method 5: Using isalpha() function
isalpha() function − Returns True if all the characters are aplhabets(a-z), else returns False.
Example
The following program returns a string after adding all the digits to the end of an input string using isalpha() function –
# 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: if(c!=' '): # checking whether current character is an alphabet if c.isalpha(): # 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:\n", resultantString)
Output
Input string: hello5691 tutorialspoint1342 Resultant string after adding digits at the end: hello tutorialspoint56911342
Conclusion
In this article, we covered 5 different methods for moving numbers to the end of a string. The same program was also implemented by us without utilizing the built-in functions.
- Related Articles
- Python program to move spaces to front of string in single traversal
- Java Program to check the end of a string
- Move all zeroes to end of the array using List Comprehension in Python
- In-place Move Zeros to End of List in Python
- How to move all the zeros to the end of the array from the given array of integer numbers using C#?
- Double the first element and move zero to end in C++ Program
- C# Program to remove the end part of a string
- Move All the Zeros to the End of Array in Java
- Program to find lexicographically smallest string to move from start to destination in Python
- Move the first row to the end of the JTable in Java Swing
- C++ program to find minimum number of steps needed to move from start to end
- Python code to move spaces to the front of string in a single traversal
- How to move the pointer of a ResultSet to the end of the table using JDBC?
- JavaScript program for Minimum move to end operations to make all strings equal
- Java Program to remove whitespace from the beginning and end of a string
