{"id":26770,"date":"2022-04-18T01:26:12","date_gmt":"2022-04-17T19:56:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=26770"},"modified":"2022-04-18T01:26:12","modified_gmt":"2022-04-17T19:56:12","slug":"how-to-separate-alphabets-and-numbers-in-a-string-using-regular-expression-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-separate-alphabets-and-numbers-in-a-string-using-regular-expression-in-python\/","title":{"rendered":"How to Separate Alphabets and Numbers in a String using regular expression in Python?"},"content":{"rendered":"
In this article, let us look at how to use regular expressions in Python to split numbers and alphabets in a number of ways. This is extremely helpful in competitive programming. The technique gets quite simple when regular expressions are used.<\/p>\n
re library\/RegEx Module:<\/strong><\/p>\n Python includes the re package, which can be used to interact with Regular Expressions. RegEx can be used to determine whether or not a string contains the specified search pattern.<\/p>\n To work with this library we should first import it.<\/p>\n re.findall() method:<\/strong><\/p>\n The findall() function to separate things out. This method gives a list of all non-overlapping matches in a string. From left to right, the string is parsed. This method takes three arguments.<\/p>\n Parameters<\/strong><\/p>\n pattern:<\/strong> It is the pattern that the user needs from the given string.<\/p>\n string:<\/strong> It is the string given as input.<\/p>\n Example1<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2: Separating Numbers and Alphabets in the specified Pattern Format<\/strong><\/p>\n Here two lists are returned, the first of which will be made up of numbers. The numbers in this list that are present without any characters in between are grouped as a single number. The same is true for the second list, which is made up of alphabets. If there are no numbers between the alphabets, they are combined together as a string.<\/p>\n Output:<\/strong><\/p>\n Example3<\/strong><\/p>\n Here it returns two lists. Only the index positions where there is a number are filled with a number in the first list; all other index places are filled with empty characters. The same holds true for the alphabets in the second list.<\/p>\n Output:<\/strong><\/p>\n In this article, let us look at how to use regular expressions in Python to split numbers and alphabets in a number of ways. This is extremely helpful in competitive programming. The technique gets quite simple when regular expressions are used. re library\/RegEx Module: Python includes the re package, which can be used to interact …<\/p>\n
\nA RegEx, or Regular Expression, is a character sequence that forms a search pattern.<\/p>\nre.findall(pattern, string, flags=0)<\/pre>\n
Separating Alphabets and Numbers in a String using regular expression in Python<\/h2>\n
\n
# Import re module using the import keyword\r\nimport re\r\n# Give the string as static input and store it in a variable\r\ngvn_str = \"Python1213prog786rams\"\r\n# Get all the numbers in a given string using findall() function\r\n# and store it in a variable.\r\n# Here '\\d'represents digits.\r\nnumbrs = re.findall(r'\\d', gvn_str)\r\n# Get all the alphabets in a given string using findall() function and\r\n# and store it in another variable.\r\n# Here '[a-zA-Z]' represents alphabets(lower, uppercase)\r\nalphabets = re.findall(r'[a-zA-Z]', gvn_str)\r\n# Print all the numbers present in a given string\r\nprint(\"The numbers present in a given string{\", gvn_str, \"}:\")\r\nprint(numbrs)\r\n# Print all the alphabets present in a given string\r\nprint(\"The alphabets present in a given string{\", gvn_str, \"}:\")\r\nprint(alphabets)\r\n<\/pre>\n
The numbers present in a given string{ Python1213prog786rams }:\r\n['1', '2', '1', '3', '7', '8', '6']\r\nThe alphabets present in a given string{ Python1213prog786rams }:\r\n['P', 'y', 't', 'h', 'o', 'n', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 's']<\/pre>\n
import re\r\ngvn_str = \"Python1213prog786rams\"\r\nnumbrs = re.findall(r'\\d+', gvn_str)\r\nalphabets = re.findall(r'[a-zA-Z]+', gvn_str)\r\n# Print all the numbers present in a given string in the specified pattern format\r\nprint(\r\n \"The numbers present in a given string{\", gvn_str, \"} in the specified pattern:\")\r\nprint(numbrs)\r\n# Print all the alphabets present in a given string in the specified pattern format\r\nprint(\r\n \"The alphabets present in a given string{\", gvn_str, \"} in the specified pattern:\")\r\nprint(alphabets)\r\n<\/pre>\n
The numbers present in a given string{ Python1213prog786rams } in the specified pattern:\r\n['1213', '786']\r\nThe alphabets present in a given string{ Python1213prog786rams } in the specified pattern:\r\n['Python', 'prog', 'rams']<\/pre>\n
import re\r\ngvn_str = \"Python1213prog786rams\"\r\nnumbrs = re.findall(r'\\d*', gvn_str)\r\nalphabets = re.findall(r'[a-zA-Z]*', gvn_str)\r\n# Print all the numbers present in a given string in the specified pattern format\r\nprint(\r\n \"The numbers present in a given string{\", gvn_str, \"} in the specified pattern:\")\r\nprint(numbrs)\r\n# Print all the alphabets present in a given string in the specified pattern format\r\nprint(\r\n \"The alphabets present in a given string{\", gvn_str, \"} in the specified pattern:\")\r\nprint(alphabets)\r\n<\/pre>\n
The numbers present in a given string{ Python1213prog786rams } in the specified pattern:\r\n['', '', '', '', '', '', '1213', '', '', '', '', '786', '', '', '', '', '']\r\nThe alphabets present in a given string{ Python1213prog786rams } in the specified pattern:\r\n['Python', '', '', '', '', 'prog', '', '', '', 'rams', '']<\/pre>\n","protected":false},"excerpt":{"rendered":"