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.

Updated on: 27-Jan-2023

579 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements