{"id":130072,"date":"2022-11-27T02:26:55","date_gmt":"2022-11-27T02:26:55","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=928531"},"modified":"2022-11-27T02:26:55","modified_gmt":"2022-11-27T02:26:55","slug":"python-split-string-empty-separator","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/11\/27\/python-split-string-empty-separator\/","title":{"rendered":"Python | Split String Empty Separator"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;928531&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> <span class=\"kksr-muted\">Rate this post<\/span> <\/div>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\"><strong>Summary:<\/strong> You can split a string using an empty separator using &#8211; <br \/><strong>(i) list constructor <br \/>(ii) map+lambda <br \/>(iii) regex <br \/>(iv) list comprehension<\/strong><\/p>\n<h3><strong>Minimal Example:<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">text = '12345' # Using list()\nprint(list(text)) # Using map+lambda\nprint(list(map(lambda c: c, text))) # Using list comprehension\nprint([x for x in text]) # Using regex\nimport re\n# Approach 1\nprint([x for x in re.split('', text) if x != ''])\n# Approach 2\nprint(re.findall('.', text))<\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Problem Formulation<\/h2>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4dc.png\" alt=\"\ud83d\udcdc\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Problem: <\/strong>How to split a string using an empty string as a separator?<\/p>\n<p><strong>Example: <\/strong>Consider the following snippet &#8211; <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">a = 'abcd'\nprint(a.split(''))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Traceback (most recent call last): File \"C:\\Users\\SHUBHAM SAYON\\PycharmProjects\\Finxter\\Blogs\\Finxter.py\", line 2, in &lt;module&gt; a.split('')\nValueError: empty separator<\/pre>\n<p><strong>Expected Output: <\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">['a', 'b', 'c', 'd']<\/pre>\n<p>So, this essentially means that when you try to split a string by using an empty string as the separator, you will get a ValueError. Thus, your task is to find out how to eliminate this error and split the string in a way such that each character of the string is separately stored as an item in a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">list<\/a>. <\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p>Now that we have a clear picture of the problem let us dive into the solutions to solve the problem.<\/p>\n<h2>Method 1: Use <a href=\"https:\/\/blog.finxter.com\/python-list\/\">list()<\/a><\/h2>\n<p><strong>Approach:\u00a0<\/strong>Use the list() constructor and pass the given string as an argument within it as the input, which will split the string into separate characters. <\/p>\n<p><strong>Note: <\/strong><code>list()<\/code>\u00a0creates a new list object that contains items obtained by iterating over the input iterable. Since a string is an iterable formed by combining a group of characters, hence, iterating over it using the\u00a0<em>list<\/em>\u00a0constructor yields a single character at each iteration which represents individual items in the newly formed list.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">a = 'abcd'\nprint(list(a)) # ['a', 'b', 'c', 'd']<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30e.png\" alt=\"\ud83c\udf0e\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>Related Read:\u00a0<strong><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list\/\" target=\"_blank\">Python list() \u2014 A Simple Guide with Video<\/a><\/strong><\/p>\n<h2>Method 2: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-map\/\" target=\"_blank\">map()<\/a> and <a href=\"https:\/\/blog.finxter.com\/a-simple-introduction-of-the-lambda-function-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">lambda<\/a><\/h2>\n<p><strong>Approach: <\/strong>Use the\u00a0<code>map()<\/code>\u00a0to execute a certain lambda function on the given string. All you need to do is to create a lambda function that simply returns the character passed to it as the input to the map object. That\u2019s it! However, the map method will return a map object, so you must convert it to a list using the\u00a0<code>list()<\/code>\u00a0function. <\/p>\n<p><strong>Code: <\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">a = 'abcd'\nprint(list(map(lambda c: c, a))) # ['a', 'b', 'c', 'd']<\/pre>\n<h2><strong>Method 3: Use a <a href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\" rel=\"noreferrer noopener\">list comprehension<\/a><\/strong><\/h2>\n<p><strong>Approach: <\/strong>Use a\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">list comprehension<\/a>\u00a0that returns a new list containing each character of the given string as individual items. <\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">a = 'abcd'\nprint([x for x in a])\n# ['a', 'b', 'c', 'd']<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30e.png\" alt=\"\ud83c\udf0e\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Related Read: <a href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\" rel=\"noreferrer noopener\">List Comprehension in Python \u2014 A Helpful Illustrated Guide<\/a><\/strong><\/p>\n<h2>Method 4: Using regex<\/h2>\n<p>The\u00a0<code>re.findall(pattern, string)<\/code>\u00a0method scans\u00a0<code>string<\/code>\u00a0from\u00a0<strong>left to right<\/strong>, searching for all\u00a0<strong>non-overlapping matches<\/strong>\u00a0of the\u00a0<code>pattern<\/code>. It returns a\u00a0<strong>list of strings<\/strong>\u00a0in the matching order when scanning the string from left to right.<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30e.png\" alt=\"\ud83c\udf0e\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Related Read: <a href=\"https:\/\/blog.finxter.com\/python-re-findall\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python re.findall() \u2013 Everything You Need to Know<\/a><\/strong><\/p>\n<p><strong>Approach: <\/strong>Use the regular expression <code>re.findall('.',a)<\/code> that finds all characters in the given string &#8216;<code>a<\/code>&#8216; and stires them in a list as individual items. <\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import re\na = 'abcd'\nprint(re.findall('.',a)) # ['a', 'b', 'c', 'd']<\/pre>\n<p>Alternatively, you can also use the split method of the regex library in a list comprehension which returns each character of the string and eliminates empty strings. <\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import re\na = 'abcd'\nprint([x for x in re.split('',a) if x!='']) # ['a', 'b', 'c', 'd']<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30e.png\" alt=\"\ud83c\udf0e\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Related Read: <a href=\"https:\/\/blog.finxter.com\/python-regex-split\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python Regex Split<\/a><\/strong><\/p>\n<p><strong><em>Do you want to master the regex superpower?<\/em><\/strong> Check out my new book <em><strong><a href=\"https:\/\/blog.finxter.com\/ebook-the-smartest-way-to-learn-python-regex\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"[eBook] The Smartest Way to Learn Python Regex\">The Smartest Way to Learn Regular Expressions in Python<\/a><\/strong><\/em> with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video. <\/p>\n<h2>Conclusion<\/h2>\n<p>Hurrah! We have successfully solved the given problem using as many as four (five, to be honest) different ways. I hope this\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\">article<\/a>\u00a0helped you and answered your queries. Please\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/subscribe\" target=\"_blank\">subscribe and stay tuned<\/a>\u00a0for more interesting articles and solutions in the future.<\/p>\n<p>Happy coding! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Regex Humor<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/image-133.png\" alt=\"\" class=\"wp-image-428862\" width=\"700\" height=\"629\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/image-133.png 785w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/image-133-300x270.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/image-133-768x691.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><figcaption><em>Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee.<\/em> (<a href=\"https:\/\/imgs.xkcd.com\/comics\/regular_expressions.png\" data-type=\"URL\" data-id=\"https:\/\/imgs.xkcd.com\/comics\/regular_expressions.png\" target=\"_blank\" rel=\"noreferrer noopener\">source<\/a>)<\/figcaption><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Rate this post Summary: You can split a string using an empty separator using &#8211; (i) list constructor (ii) map+lambda (iii) regex (iv) list comprehension Minimal Example: text = &#8216;12345&#8217; # Using list() print(list(text)) # Using map+lambda print(list(map(lambda c: c, text))) # Using list comprehension print([x for x in text]) # Using regex import re [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-130072","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=130072"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130072\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=130072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=130072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=130072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}