# How to extract a substring from a string using position and length

**URL:** <https://discourse.processing.org/t/how-to-extract-a-substring-from-a-string-using-position-and-length/12536>\
**Category:** Processing.py\
**Created:** [July 6, 2019, 1:10pm UTC](https://discourse.processing.org/t/how-to-extract-a-substring-from-a-string-using-position-and-length/12536 "2019-07-06T13:10:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [July 6, 2019, 1:10pm UTC](https://discourse.processing.org/t/how-to-extract-a-substring-from-a-string-using-position-and-length/12536/1 "2019-07-06T13:10:32Z")

</div>

Hello,  
Example:  
string1 = ‘abcdefghijk’  
I need to extract a substring from 4th position for a length of 2 characters  
I tried this Python code and I get a syntax error:  
substring1 = string1(3:2)  
(the result in substring should be ‘de’ if it works)

Thanks for help.

---

<div class="post-metadata">

**Author:** ![The\_nish](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the_nish/32/5634_2.png) [@The\_nish](https://discourse.processing.org/u/The_nish)\
**Post date:** [July 6, 2019, 1:30pm UTC](https://discourse.processing.org/t/how-to-extract-a-substring-from-a-string-using-position-and-length/12536/2 "2019-07-06T13:30:37Z")

</div>

try this

String s = (String1[3:4]); // Returns “de”

---

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [July 6, 2019, 1:46pm UTC](https://discourse.processing.org/t/how-to-extract-a-substring-from-a-string-using-position-and-length/12536/3 "2019-07-06T13:46:02Z")

</div>

Thanks, it works with [3:5] (as [3:4] returns only ‘d’).

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 6, 2019, 1:50pm UTC](https://discourse.processing.org/t/how-to-extract-a-substring-from-a-string-using-position-and-length/12536/4 "2019-07-06T13:50:24Z")

</div>

> [@proc8888](#):
>
> I need to extract a substring from 4th position for a length of 2 characters.

```python
pos4th = 4 - 1 # 3
len2 = pos4th + 2 # 5

abc = 'abcdefghijk'
de = abc[pos4th:len2]

print abc, de, ENTER, pos4th, len2
exit()

```

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 6, 2019, 5:51pm UTC](https://discourse.processing.org/t/how-to-extract-a-substring-from-a-string-using-position-and-length/12536/5 "2019-07-06T17:51:27Z")

</div>

Substring function in python extract sub strings with the sense of StartIndex , EndIndex-1

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 6, 2019, 6:11pm UTC](https://discourse.processing.org/t/how-to-extract-a-substring-from-a-string-using-position-and-length/12536/6 "2019-07-06T18:11:29Z")

</div>

Indeed, left side value of `:` is inclusive; while right side value is exclusive. 🤓

---

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [July 7, 2019, 5:50am UTC](https://discourse.processing.org/t/how-to-extract-a-substring-from-a-string-using-position-and-length/12536/7 "2019-07-07T05:50:02Z")

</div>

Found more ‘slice’ options for future use 🙂  
[https://py.processing.org/reference/slice.html](https://py.processing.org/reference/slice.html)
