# Check for letters-only string

**URL:** https://discourse.julialang.org/t/check-for-letters-only-string/41091
**Category:** New to Julia
**Created:** [June 9, 2020, 8:11pm UTC](https://discourse.julialang.org/t/check-for-letters-only-string/41091 "2020-06-09T20:11:55Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Orr\_Orovan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orr_orovan/32/15538_2.png) [@Orr\_Orovan](https://discourse.julialang.org/u/Orr_Orovan)
#### Post date: [June 9, 2020, 8:11pm UTC](https://discourse.julialang.org/t/check-for-letters-only-string/41091/1 "2020-06-09T20:11:55Z")

</div>

Python can check whether a string contain only letters with isalpha():

> > > a\_few = “abcdefghijk”  
> > > a\_few.isalpha()  
> > > True  
> > > mixed = “abc45defg”  
> > > mixed.isalpha()  
> > > False

I have found Julia’s isletter() but it is limited to checking a single character.  
How can Julia check for letters-only string?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 9, 2020, 8:31pm UTC](https://discourse.julialang.org/t/check-for-letters-only-string/41091/2 "2020-06-09T20:31:53Z")

</div>

You can apply `isletter` to all the characters in a string like so:

```julia
julia> all(isletter, test)
false

julia> a_few = "asfeaer"
"asfeaer"

julia> mixed = "adasd34asda"
"adasd34asda"

julia> all(isletter, a_few)
true

julia> all(isletter, mixed)
false

```

---

<div class="post-metadata">

### Author: ![Orr\_Orovan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orr_orovan/32/15538_2.png) [@Orr\_Orovan](https://discourse.julialang.org/u/Orr_Orovan)
#### Post date: [June 9, 2020, 9:11pm UTC](https://discourse.julialang.org/t/check-for-letters-only-string/41091/3 "2020-06-09T21:11:44Z")

</div>

Thank you!!

Works well for what I was looking for.

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [June 9, 2020, 9:15pm UTC](https://discourse.julialang.org/t/check-for-letters-only-string/41091/4 "2020-06-09T21:15:06Z")

</div>

I am not sure how much you care about performance or unicode-ness, but this will check if the letters are a to z and it’s around 4x faster that the more mature version:

```julia
julia> text = "a" ^ 1_000_000;

julia> @btime all(isletter, $text)
  6.489 ms (0 allocations: 0 bytes)
true

julia> function isatoz(text)
           all(c -> 0x61 <= UInt8(c) <= 0x7A, text)
       end
isatoz (generic function with 1 method)

julia> @btime isatoz($text)
  1.774 ms (0 allocations: 0 bytes)
true

```

---

<div class="post-metadata">

### Author: ![Orr\_Orovan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orr_orovan/32/15538_2.png) [@Orr\_Orovan](https://discourse.julialang.org/u/Orr_Orovan)
#### Post date: [June 9, 2020, 9:44pm UTC](https://discourse.julialang.org/t/check-for-letters-only-string/41091/5 "2020-06-09T21:44:45Z")

</div>

Very interesting.

In the particular case I was working on speed does not count but I like the transparency and preciseness of what you are suggesting here. I don’t know what is under the hood of isletter(). The function you define here is not leaving much room for doubt.

Thank you too!!

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [June 9, 2020, 10:00pm UTC](https://discourse.julialang.org/t/check-for-letters-only-string/41091/6 "2020-06-09T22:00:45Z")

</div>

You can try `@edit isletter('a')` in the Julia REPL, it should open up your default editor with the sources of that method. It’s very helpful to see what’s under the hood.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [June 9, 2020, 10:10pm UTC](https://discourse.julialang.org/t/check-for-letters-only-string/41091/7 "2020-06-09T22:10:23Z")

</div>

The difference is that `isletter` not only detects letters `A-z`, but unicode letters from other alphabets as well, for example accented letters, umlauts or greek letters… Naturally, this is quite a bit more complicated, but it’s important if you want to support languages other than English.

---

<div class="post-metadata">

### Author: ![Orr\_Orovan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orr_orovan/32/15538_2.png) [@Orr\_Orovan](https://discourse.julialang.org/u/Orr_Orovan)
#### Post date: [June 9, 2020, 10:27pm UTC](https://discourse.julialang.org/t/check-for-letters-only-string/41091/8 "2020-06-09T22:27:19Z")

</div>

I checked what @tamasgal suggested —\>\> @edit isletter(‘a’) to see its structure. Did not know of course, that this is available.  
I can see how isletter() will pick up letters that are not English as you mention here. This distinction makes it not proper for the application I was working on. I should have instead define a function like @tamasgal demonstrated because the user input it handles must be English letters and isletter() was used in a data validation step to verify that it is … an English letter.

Appreciate very much the valuable input from you all.  
Thanks again!

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [June 9, 2020, 10:43pm UTC](https://discourse.julialang.org/t/check-for-letters-only-string/41091/9 "2020-06-09T22:43:01Z")

</div>

Note that @tamasgal’s example only detects lowercase letters. To also detect uppercase letter, you also need to check against `0x41` through `0x5a`. It’s probably also easier to understand if you write this as `'a' <= c <= 'z' || 'A' <= c <= 'Z'`, so you don’t convert to`UInt8` first and have to remember all the ASCII codes, since you can just compare `Char`s directly.

---

<div class="post-metadata">

### Author: ![Orr\_Orovan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orr_orovan/32/15538_2.png) [@Orr\_Orovan](https://discourse.julialang.org/u/Orr_Orovan)
#### Post date: [June 10, 2020, 2:04am UTC](https://discourse.julialang.org/t/check-for-letters-only-string/41091/10 "2020-06-10T02:04:38Z")

</div>

Excellent point. I passed lowercase() on the user input to avoid this problem but writing it like you do removes this concern. Using the actual characters instead of their corresponding codes is clearer to read.  
If I had to choose ASCII code or character the next time I need it I would look for a drive or certain circumstances that prefer one over the other. I don’t know what it can be if any.
