
- 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 – Split Numeric String into K digit integers
When it is required to split a numeric string into K digit integers, a simple iteration, the ‘int’ method and the ‘append’ methods are used.
Example
Below is a demonstration of the same −
my_string = '69426874124863145' print("The string is : " ) print(my_string) K = 4 print("The value of K is ") print(K) my_result = [] for index in range(0, len(my_string), K): my_result.append(int(my_string[index : index + K])) print("The resultant list is : ") print(my_result) print("The resultant list after sorting is : ") my_result.sort() print(my_result)
Output
The string is : 69426874124863145 The value of K is 4 The resultant list is : [6942, 6874, 1248, 6314, 5] The resultant list after sorting is : [5, 1248, 6314, 6874, 6942]
Explanation
A string is defined and is displayed on the console.
The value of K is defined and is displayed on the console.
An empty list is defined.
The list is iterated over, and the elements in the string within a specific range are converted into an integer.
This value is appended to the empty list.
This is displayed as the output on the console.
This list is sorted again and is displayed on the console.
- Related Articles
- Python program to split string into k distinct partitions
- Split string into groups - JavaScript
- Split string into equal parts JavaScript
- Python – Reform K digit elements
- Program to split lists into strictly increasing sublists of size greater than k in Python
- C# program to split the Even and Odd integers into different arrays
- Split string into sentences using regex in PHP
- Check if given string can be split into four distinct strings in Python
- Python – Average of digit greater than K
- Extract tuples having K digit elements in Python
- Program to find split a string into the max number of unique substrings in Python
- Program to check a string can be split into three palindromes or not in Python
- Program to check whether we can split a string into descending consecutive values in Python
- How to split a Java String into tokens with StringTokenizer?
- How to split a String into an array in Kotlin?

Advertisements