Python - Splitting Text and Number in string Last Updated : 20 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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 method uses a simple for loop to iterate through the string. Each character is checked to determine whether it is a letter or a digit using basic conditional statements. Python # Initializing the input string a = "abc123" # Initializing variables for text and number s = "" n = "" # Iterating through each character in the string for c in a: if c.isdigit(): # Checking if character is a digit n += c # Adding to number else: s += c # Adding to text print(s, n) Outputabc 123 Explanation:We initialize two variables, one for storing text and the other for numbers.Each character is checked using isdigit() to decide whether it belongs to the text or the number part.Using list comprehensionsThis method uses list comprehensions by iterating over the string, we create two separate lists: one for the letters and another for the numbers. These are then joined together to form the final outputs. Python # Initializing the input string a = "abc123" # Splitting the string using list comprehensions s = "".join([c for c in a if not c.isdigit()]) # Getting letters n = "".join([c for c in a if c.isdigit()]) # Getting numbers print(s, n) Outputabc 123 Explanation:Two separate list comprehensions are used to filter letters and numbers.The join() function is used to combine the filtered characters into strings.Using regexRegular expressions (regex) provide a versatile way to split strings. In this method, we use patterns to identify the letter and number components of the string. Regex is particularly useful for handling more complex strings with additional patterns. Python import re # Initializing the input string a = "abc123" # Splitting the string using regex s = re.findall("[a-zA-Z]+", a)[0] # Extracting letters n = re.findall("[0-9]+", a)[0] # Extracting numbers print(s, n) Outputabc 123 Explanation:Regular expressions "[a-zA-Z]+" and "[0-9]+" are used to match letters and numbers.This method is effective for more complex patterns but adds overhead due to the regex library.Using itertools.groupbygroupby() method from the itertools module groups consecutive elements based on a specified condition. Here, we group characters by whether they are digits or not. Python from itertools import groupby # Initializing the input string a = "abc123" # Splitting the string using groupby groups = ["".join(g) for k, g in groupby(a, key=str.isdigit)] # Grouping by type s = groups[0] # Extracting text n = groups[1] # Extracting numbers print(s, n) Outputabc 123 Explanation:groupby() function groups consecutive characters based on whether they are digits or not.Groups are combined into strings to get the text and number parts. Comment More infoAdvertise with us Next Article Python - Splitting Text and Number in string manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Split and join a string in Python The goal here is to split a string into smaller parts based on a delimiter and then join those parts back together with a different delimiter. For example, given the string "Hello, how are you?", you might want to split it by spaces to get a list of individual words and then join them back together 3 min read Python - Retain Numbers in String Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones.Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to for 2 min read Python | Splitting operators in String Sometimes we have a source string to have certain mathematical statement for computations and we need to split both the numbers and operators as a list of individual elements. Let's discuss certain ways in which this problem can be performed. Method #1 : Using re.split() This task can be solved usin 7 min read Insert a number in string - Python We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The num 2 min read Splitting String to List of Characters - Python We are given a string, and our task is to split it into a list where each element is an individual character. For example, if the input string is "hello", the output should be ['h', 'e', 'l', 'l', 'o']. Let's discuss various ways to do this in Python.Using list()The simplest way to split a string in 2 min read Python | Split strings and digits from string list Sometimes, while working with String list, we can have a problem in which we need to remove the surrounding stray characters or noise from list of digits. This can be in form of Currency prefix, signs of numbers etc. Let's discuss a way in which this task can be performed. Method #1 : Using list com 5 min read Separate Alphabets and Numbers in a String - Python The task is to separate alphabets and numbers from a string. For example, given "a1b2c3", the output should be alphabets "abc" and numbers "123".Using List ComprehensionList comprehension offers a concise and efficient way to separate alphabets and numbers from a string. It iterates through each cha 2 min read Python - Split Numeric String into K digit integers Given a String, convert it to K digit integers Input : test_str = '457336', K = 2 Output : [45, 73, 36] Explanation : Divided in 2 digit integers. Input : test_str = '457336', K = 3 Output : [457, 336] Explanation : Divided in 3 digit integers. Method #1 : Using int() + slice + loop In this, we iter 5 min read Python | Check Numeric Suffix in String Sometimes, while programming, we can have such a problem in which we need to check if any string is ending with a number i.e it has a numeric suffix. This problem can occur in Web Development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using regex This problem 6 min read Python program to split a string by the given list of strings Given a list of strings. The task is to split the string by the given list of strings. Input : test_str = 'geekforgeeksbestforgeeks', sub_list = ["best"] Output : ['geekforgeeks', 'best', 'forgeeks'] Explanation : "best" is extracted as different list element. Input : test_str = 'geekforgeeksbestfor 4 min read Like