# Iterating over String changes its Structure to a an array Characters

**URL:** https://discourse.julialang.org/t/iterating-over-string-changes-its-structure-to-a-an-array-characters/49417
**Category:** General Usage
**Tags:** strings
**Created:** [November 1, 2020, 4:25pm UTC](https://discourse.julialang.org/t/iterating-over-string-changes-its-structure-to-a-an-array-characters/49417 "2020-11-01T16:25:13Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Jim\_Beck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jim_beck/32/19111_2.png) [@Jim\_Beck](https://discourse.julialang.org/u/Jim_Beck)
#### Post date: [November 1, 2020, 4:25pm UTC](https://discourse.julialang.org/t/iterating-over-string-changes-its-structure-to-a-an-array-characters/49417/1 "2020-11-01T16:25:13Z")

</div>

I have a list of strings that I’m attempting to compare to a unique other string. I have a function that compares each string character by character, counting differences at each location.

```julia
function compareStrings(sequence₁, sequence₂)
    count = 0;
    for i in 1:length(sequence₁)
        if sequence₁[i] != sequence₂[i]
            count+=1;
        end
    end
    return count
end

```

I then pass the strings like:

```julia
test_array = [];

for i in unique_string_list
    if compareStrings(WT,i) == 2
        append!(test_array,i)
    end
end

```

I’m trying to build up an array of strings that have 2 differences. But when I attempt to append to the array, I just get a list of all the characters in the string.

 ![Screen Shot 2020-11-01 at 9.21.43 AM](https://global.discourse-cdn.com/julialang/original/3X/9/d/9db913005aaff8801ecf4fd8de8404ab00d6d1cd.png)

It seems like the iteration through the string is changing the string into a list of characters and I have been unable to get the string back to a state that allows me to append it to the array.

Any thoughts on this behavior?

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 1, 2020, 4:42pm UTC](https://discourse.julialang.org/t/iterating-over-string-changes-its-structure-to-a-an-array-characters/49417/2 "2020-11-01T16:42:23Z")

</div>

The `append!` function appends one collection to another one — you want the `push!` function. Note that this deviates from Python, but Python is the outlier here: Julia uses the standard meanings of these functions in most programming languages (Lisp, Perl, Ruby, etc.).

---

<div class="post-metadata">

### Author: ![Jim\_Beck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jim_beck/32/19111_2.png) [@Jim\_Beck](https://discourse.julialang.org/u/Jim_Beck)
#### Post date: [November 1, 2020, 4:54pm UTC](https://discourse.julialang.org/t/iterating-over-string-changes-its-structure-to-a-an-array-characters/49417/3 "2020-11-01T16:54:57Z")

</div>

Thank you. Easy as pie!

---

<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: [November 2, 2020, 5:59pm UTC](https://discourse.julialang.org/t/iterating-over-string-changes-its-structure-to-a-an-array-characters/49417/4 "2020-11-02T17:59:01Z")

</div>

> [@Jim\_Beck](#):
>
> ```julia
> function compareStrings(sequence₁, sequence₂)
> count = 0;
> for i in 1:length(sequence₁)
> if sequence₁[i] != sequence₂[i]
> count+=1;
> end
> end
> return count
> end
> 
> ```

This code will only work for ASCII strings, since [string indices are not consecutive in general](https://docs.julialang.org/en/v1/manual/strings/#Unicode-and-UTF-8)—it will throw an error for strings containing other Unicode characters. (You need to use `nextind` to increment the index.) The easiest solution is probably to use `zip`:

```julia
compare_strings(s1, s2) = count(((a,b),) -> a != b, zip(s1, s2))

```

Note that, similar your original function, if `s1` and `s2` have different lengths this function will only look at the characters they have in common. i.e. it only looks at `min(length(s1), length(s2))` characters. You could add `abs(length(s1) - length(s2))` to the result if you want to count differences in length.

---

<div class="post-metadata">

### Author: ![Jim\_Beck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jim_beck/32/19111_2.png) [@Jim\_Beck](https://discourse.julialang.org/u/Jim_Beck)
#### Post date: [November 3, 2020, 12:25am UTC](https://discourse.julialang.org/t/iterating-over-string-changes-its-structure-to-a-an-array-characters/49417/5 "2020-11-03T00:25:46Z")

</div>

Did not even think about zip. Thanks for the suggestion.

---

<div class="post-metadata">

### Author: ![Jim\_Beck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jim_beck/32/19111_2.png) [@Jim\_Beck](https://discourse.julialang.org/u/Jim_Beck)
#### Post date: [November 3, 2020, 2:18pm UTC](https://discourse.julialang.org/t/iterating-over-string-changes-its-structure-to-a-an-array-characters/49417/6 "2020-11-03T14:18:35Z")

</div>

I’m curious about the syntax ((a,b),) - I can’t find in the documentation what that extra common does to the tupel (a,b).

JB

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [November 3, 2020, 3:37pm UTC](https://discourse.julialang.org/t/iterating-over-string-changes-its-structure-to-a-an-array-characters/49417/7 "2020-11-03T15:37:02Z")

</div>

The syntax `(x,)` denotes a tuple with the single element `x`, so `((a,b),)` is a 1-tuple having `(a,b)` as its single element.  
In the context of an anonymous function expression, the arguments are given in a tuple, so `(a,b)->a!=b` would denote a function with two arguments (named `a` and `b`). `((a,b),)->a!=b` is a 1-argument function where the first argument is destructured as `(a,b)` ([Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#Argument-destructuring)).  
Incidentally, you can also use `splat` to get an equivalent 1-argument function:

```julia
count(Base.splat(!=), zip(s1, s2))

```
