# Problem with split() command

**URL:** <https://discuss.python.org/t/problem-with-split-command/25415>\
**Category:** Python Help\
**Tags:** stdlib\
**Created:** [April 3, 2023, 6:22am UTC](https://discuss.python.org/t/problem-with-split-command/25415 "2023-04-03T06:22:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Albert](https://discuss.python.org/u/Albert)\
**Post date:** [April 3, 2023, 6:22am UTC](https://discuss.python.org/t/problem-with-split-command/25415/1 "2023-04-03T06:22:36Z")

</div>

I have run below statement

\</\>  
x = ‘d:\level1\test.exe’  
print(x.isspace())  
result = x.split(‘\’)  
for i in result:  
print(i)  
\</\>

and expected the come out is:  
False  
d:  
level1  
test.exe

but finally the come out is:

False  
d:  
level1 est.exe

What is the problem? It looks like the characters ‘\t’ recognized as [TAB]. How can I correct the problem?

I am using Python 3.9 with PyCharm 2022.3.2

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [April 3, 2023, 6:34am UTC](https://discuss.python.org/t/problem-with-split-command/25415/2 "2023-04-03T06:34:50Z")

</div>

Please edit your code (pencil icon) to replace `</>` with

````auto
```

````

It does not read right as is.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [April 3, 2023, 6:54am UTC](https://discuss.python.org/t/problem-with-split-command/25415/3 "2023-04-03T06:54:30Z")

</div>

> [@Albert](#):
>
> It looks like the characters ‘\t’ recognized as [TAB].

Yes, that is the problem. It has nothing to do with `.split`, the string is not created properly in the first place.

> [@Albert](#):
>
> How can I correct the problem?

By creating the string properly, [using escape sequences](https://docs.python.org/3/reference/lexical_analysis.html#strings) where needed. Specifically, [to put an actual backslash in the string, double it up in the source code](https://stackoverflow.com/questions/3380484). Note that you must also use this for the delimiter for the `.split` call: `x.split('\\')`. The code that you showed should not be accepted at all (but maybe it just got messed up when you tried to post it).

However, you should also be aware that [on Windows, it is not necessary, and generally not a good idea, to use backslashes for file paths](https://stackoverflow.com/questions/2953834/) even though that is what DOS uses. Just write a forward slash; it will be translated for you automatically behind the scenes.

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [April 3, 2023, 7:33am UTC](https://discuss.python.org/t/problem-with-split-command/25415/4 "2023-04-03T07:33:05Z")

</div>

In addition: if you are trying to work with pathnames, then you should use the [os.path](https://docs.python.org/3/library/os.path.html?highlight=os%20path#module-os.path) module.

---

<div class="post-metadata">

**Author:** ![abessman](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abessman/32/8367_2.png) [@abessman](https://discuss.python.org/u/abessman)\
**Post date:** [April 3, 2023, 7:38am UTC](https://discuss.python.org/t/problem-with-split-command/25415/5 "2023-04-03T07:38:21Z")

</div>

Or [pathlib](https://docs.python.org/3/library/pathlib.html), which is newer and offers a higher-level interface.

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [April 3, 2023, 11:12pm UTC](https://discuss.python.org/t/problem-with-split-command/25415/6 "2023-04-03T23:12:36Z")

</div>

Correct.

In addition to the various suggestions already, if I working with  
strings containing backslashes such as Windows pathnames or regular  
expressions I try to use 'raw strings", which prefix the leading quote  
with an “r”, example:

```
 path = r'd:\foo\text.txt'

```

Inside a raw string the backslash is not special. This means:

- you do not need to double them to get a backslash
- you don’t need to handle eg `\t` specially

A “raw string” is just Python code notation - the result is still a  
normal string.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![Albert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Albert](https://discuss.python.org/u/Albert)\
**Post date:** [April 5, 2023, 11:04am UTC](https://discuss.python.org/t/problem-with-split-command/25415/7 "2023-04-05T11:04:50Z")

</div>

Problem solved. Thanks.😀😀

Cameron Simpson via Discussions on [Python.org](http://Python.org) \<[notifications@python1.discoursemail.com](mailto:notifications@python1.discoursemail.com)\> 於 2023年4月4日 週二 上午7:22寫道：
