# String and list

**URL:** <https://discourse.julialang.org/t/string-and-list/37464>\
**Category:** General Usage\
**Tags:** strings\
**Created:** [April 12, 2020, 8:57pm UTC](https://discourse.julialang.org/t/string-and-list/37464 "2020-04-12T20:57:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marques](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marques/32/13630_2.png) [@Marques](https://discourse.julialang.org/u/Marques)\
**Post date:** [April 12, 2020, 8:57pm UTC](https://discourse.julialang.org/t/string-and-list/37464/1 "2020-04-12T20:57:50Z")

</div>

Hi guys. I searched but I couldn’t find the answer. I would like to know how to invert the reading mode, for example in python if you have the phrase = “lua”, just make the phrase [-1: -4: -1], and the result would be aul. But I don’t know how to do this on Julia. Can anybody help me?. Thanks 🙂

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [April 12, 2020, 9:01pm UTC](https://discourse.julialang.org/t/string-and-list/37464/2 "2020-04-12T21:01:35Z")

</div>

```julia
julia> s="hey"
"hey"

julia> s[end:-1:begin]
"yeh"

julia>    

```

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [April 12, 2020, 9:26pm UTC](https://discourse.julialang.org/t/string-and-list/37464/3 "2020-04-12T21:26:16Z")

</div>

Another solution is `reverse("lua")`

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 12, 2020, 9:26pm UTC](https://discourse.julialang.org/t/string-and-list/37464/4 "2020-04-12T21:26:28Z")

</div>

> [@PetrKryslUCSD](#):
>
> ```julia
> s[end:-1:begin]
> 
> ```

This will fail for non-ascii strings:

```julia
julia> s = "αβγδϵhelloω"
"αβγδϵhelloω"

julia> s[end:-1:begin]
ERROR: StringIndexError("αβγδϵhelloω", 10)

```

Instead use `reverse`:

```julia
julia> reverse(s)
"ωollehϵδγβα"

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 13, 2020, 12:14pm UTC](https://discourse.julialang.org/t/string-and-list/37464/5 "2020-04-13T12:14:38Z")

</div>

If you want to reverse indices, you can also use `Iterators.reverse` on `eachindex(s)`. In general, `Iterators.reverse` is the **non-allocating** way to reverse a collection. (In contrast, `Base.reverse` allocates the reversed collection, but works on fewer iterable types.)

```julia
julia> s = "αβγδϵhelloω"
"αβγδϵhelloω"

julia> [s[i] for i in Iterators.reverse(eachindex(s))]
11-element Array{Char,1}:
 'ω'
 'o'
 'l'
 'l'
 'e'
 'h'
 'ϵ'
 'δ'
 'γ'
 'β'
 'α'

julia> join(s[i] for i in Iterators.reverse(eachindex(s)))
"ωollehϵδγβα"

julia> collect(Iterators.reverse(eachindex(s)))
11-element Array{Int64,1}:
 16
 15
 14
 13
 12
 11
  9
  7
  5
  3
  1

```
