# using every other character in a variable

**URL:** <https://forum.ansible.com/t/using-every-other-character-in-a-variable/38427>\
**Category:** Ansible Project\
**Created:** [September 12, 2023, 1:35pm UTC](https://forum.ansible.com/t/using-every-other-character-in-a-variable/38427 "2023-09-12T13:35:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [September 12, 2023, 1:35pm UTC](https://forum.ansible.com/t/using-every-other-character-in-a-variable/38427/1 "2023-09-12T13:35:14Z")

</div>

I am trying to grab every other character in a very long variable, and placing it in another variable. This is what I have:

set\_fact:

new\_string: “{{ [0,2,4,6,8,10,12] | map(‘extract’, verylongvariable) }}”

I’ve also tried this:  
set\_fact:  
new\_string: "{{ [0,2,4,6,8,10,12] | map(‘extract’, verylongvariable) }} | map(‘join’, ‘,’ | join(‘’)) }}

What I end up getting in the new\_string is this:

‘N’, ‘y’, ‘a’, ‘d’…

What I want is:

Nyad…

I clearly am not understanding the join syntax. How do I use join to get the new variable?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [September 12, 2023, 1:39pm UTC](https://forum.ansible.com/t/using-every-other-character-in-a-variable/38427/2 "2023-09-12T13:39:16Z")

</div>

All you need is to use is slicing with a step:

If my variable is `verylongvariable: s1i2v3e4l5`

If I want `sivel`, I’d use: {{verylongvariable[::2]}}

If I want `12345`, I’d use: {{verylongvariable[1::2]}}

---

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [September 12, 2023, 2:06pm UTC](https://forum.ansible.com/t/using-every-other-character-in-a-variable/38427/3 "2023-09-12T14:06:28Z")

</div>

Great thank you! Is there documentation somewhere on using ‘::’ ? I’ve looked everywhere…

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 12, 2023, 2:16pm UTC](https://forum.ansible.com/t/using-every-other-character-in-a-variable/38427/4 "2023-09-12T14:16:23Z")

</div>

You just need a join to make your current expression's output (list)  
into a string.  
But @sivel's solution with slicing is probably better, the reason you  
cannot find it in documentation is because this is a "Pythonism" and a  
property of Python list objects.  
Ansible uses Jinja for expression evaluation, Jinja compiles into  
Python in the end and exposes many of the properties of those Python  
objects, neither Ansible nor Jinja document these properties so you  
would need to a) know the type of object you are dealing with and b)  
look at python documentation for those objects.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [September 12, 2023, 2:19pm UTC](https://forum.ansible.com/t/using-every-other-character-in-a-variable/38427/5 "2023-09-12T14:19:14Z")

</div>

You can find the information about slicing within the python docs at: [https://docs.python.org/3/library/functions.html#slice](https://docs.python.org/3/library/functions.html#slice)

Probably some other docs spread around as well that go into more depth.

---

<div class="post-metadata">

**Author:** ![shearn](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@shearn](https://forum.ansible.com/u/shearn)\
**Post date:** [September 12, 2023, 2:33pm UTC](https://forum.ansible.com/t/using-every-other-character-in-a-variable/38427/6 "2023-09-12T14:33:45Z")

</div>

What are some good resources to learn Ansible "pythonism" filters? I struggle with string manipulation, sets, dict merging, etc. In my opinion this is one of the greatest strengths of Ansible.

How did you guys learn it? Did you learn it because you knew python first?

Regards,  
Stan
