Write a program or function that prints or returns a string of the alphanumeric characters plus underscore, in any order. To be precise, the following characters need to be output, and no more:


When printing to stdout, an optional trailing newline after your output is permitted.

Built-in constants that contain 9 or more of the above characters are disallowed.

Shortest code in bytes wins.

This is a very simple challenge, which I believe will generate some interesting answers nevertheless.


Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

var QUESTION_ID=85666,OVERRIDE_USER=4162;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

  • 2
    \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$
    – Dennis
    Jul 19 '16 at 0:45
  • \$\begingroup\$ "Built-in constants that contain 9 or more of the above characters are disallowed" sighs in 05AB1E... \$\endgroup\$
    – Makonede
    Feb 24 '21 at 3:43

94 Answers 94


K, 28 bytes

_ci,/(97 65+\:!26),95,48+!10

Generates [97..122]++[65..90]++[95]++[48..57] and maps the numbers to chars.


SMBF, 29 bytes


Try it online

The last byte is a literal \x1A (decimal 26). It shows in the "edit" mode of this answer as a tiny arrow, but cannot otherwise be seen for some reason...

I use literals in the source code to provide a starting value and how many times to loop. 26 times for the loop printing Z-A and z-a, then subtract and print _, then use the newline (decimal 10) to print 9 and subtract, looping 10 times.



Keg, 15B(The Keg SBCS is in Keg wiki.)



\_#             Push _
  \0\9ɧ#        Push range 0-9
       ZAɧ#     Push range Z-A
          zaɧ#  Push range z-a



x86-16 machine code, PC DOS, 26 bytes


00000000: be12 01ad 918a c1cd 293a cde0 f83c 5f75  ........):...<_u
00000010: f2c3 7a61 5a41 3930 5f5f                 ..zaZA90__


BE 0112     MOV  SI, OFFSET TBL     ; SI to range table
AD          LODSW                   ; AL = high, AH = low
91          XCHG AX, CX             ; move to CX
8A C1       MOV  AL, CL             ; move current char to AL for display
CD 29       INT  29H                ; write to console
3A CD       CMP  CL, CH             ; high and low char the same yet?
E0 F8       LOOPNZ CLOOP            ; if not, decrement and keep looping
3C 5F       CMP  AL, '_'            ; was last char displayed a '_'?
75 F2       JNZ  RLOOP              ; loop until then
C3          RET                     ; return to DOS
        TBL DB 'zaZA90__'           ; handy table of ranges to display

Uses a table with a range of chars to display. Turned out that it wasn't any longer to do it this way than unroll all of the 4 different conditions in code (though I'm positive I can be proven wrong there).

Standalone PC DOS executable (COM) program.


enter image description here


ZX Spectrum, (Machine Code) 27 bytes

start in BASIC with PRINT "" AND USR 4e4

      org 40000
      dump 40000

      ld b,"z"
      ld a,"_"
      rst 16
nchar ld a,b
      cp "/"
      ret z
      cp ":"
      jr c,ok ; print numbers
      sub "A"
      cp "Z"+1-“A”
      ld a,b ; undo change
      jr c,ok ; print A-Z
      cp "a"
      jr c,fnext ; in between ranges
ok    rst 16
fnext djnz nchar



Output: _zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA9876543210

  • \$\begingroup\$ Can we have a hexdump of the compiled version? \$\endgroup\$ Jul 19 '16 at 17:37
  • \$\begingroup\$ Will make it later. \$\endgroup\$ Jul 19 '16 at 21:02



I believe this does not count as letters and LETTERS are built-in constants. But this one should be fine:


Actually, 24 bytes


Try it online!

There's probably a shorter way


       "A{"O              push a list containing the ordinals of "A" and "{"
            ix            flatten the list, end-exclusive range
              ♂c          convert each ordinal into its corresponding character
"[\]^`"         -         remove non-alphabetic characters except "_"
                 9ur♂$+   push range(10), convert to strings, concatenate lists
                       Σ  concatenate strings in the list

Perl 6

put |grep /<alnum>/,'0'..'z'
put |grep /\w/,'0'..'z'


put # 「print()」 but with trailing newline

# turn the following into a Slip so that there aren't 
# any spaces in the output

  /\w/,      # match wordchars
  '0' .. 'z' # in this Range

( grep returns a List, which puts spaces between each element when stringified )


Erlang, 56 bytes


Erlang, 57 bytes

f()->S=fun lists:seq/2,[$_|S($0,$9)]++S($A,$Z)++S($a,$z).


Excel VBA, 130 bytes

Have fun hitting enter...

Sub q()
For i = 1 To 123
If i > 47 And i < 58 Or i > 64 And i < 91 Or i > 96 And i < 123 Or i = 95 Then MsgBox Chr(i)
End Sub

brainfuck, 105

first code golf attempt using this



>++++++[<++++++++>-]         // p1=48
++++++++++                   // p2=10
[-<.+>]<                     // print 0-9, p1=58, p2=0
+++++++                      // p1=65
>>+++++[<+++++>-]<+          // p2=26
[-<.+>]<                     // print A-Z, p1=91, p2=0
++++.++                      // print underscore, p1=97 
>>+++++[<+++++>-]<+          // p2=26
[-<.+>]                      // print a-z
  • \$\begingroup\$ ++++++++++++++++++++++++++++++++++++++++++++++++ That can be done better. \$\endgroup\$
    – orlp
    Jul 18 '16 at 20:30
  • \$\begingroup\$ improved the +x26 as well \$\endgroup\$
    – Kevin L
    Jul 19 '16 at 13:23

Javascript (Using external library) (105 bytes)


What this does is gets the char code ranges from a-z, concats with A-Z, then concats 0-9, then pushes the code for _. Write takes a delimiter and joins everything together

Library: https://github.com/mvegh1/Enumerable/blob/master/linq.js

enter image description here


PHP, 74 71 bytes

echo 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';

Brainfuck - 116 commands


Output: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz

How it works

>++++++++[<++++++>-] Get yourself up to \48, the start of [0-9]

>+++[<+++>-]<+ Count up 10

[<.+>-] Then print each character and bump it up

++[<+++>-]<+> jump up to the start of [A-Z]

>++++[<++++++>-]<++ start a counter of 26

[<.+>-] and print and increment 26 values

<++++.++> jump to [a-z], stopping in at _

>++++[<++++++>-]<++ counter of 26

[<.+>-] and print and increment 26 values

there's got to be a way to simplify that counter to 26 and the print happening twice, but I'm not nearly good enough with brainfuck to do so

  • \$\begingroup\$ 48 doubled is 96, so you could use replace the initial loop with >++++[>++++++[>++>++++<<-]<-] to get both. Probably shorter than two output loops. \$\endgroup\$
    – primo
    Jul 20 '16 at 9:23

dc, 50 bytes


This doesn't feel very elegant.

[              # Open macro definition
 la 1+         #  Load a, increment
 dd            #  Duplicate a+1 twice
 sa            #  Store a+1 as `a'
 P             #  Print new value of a as a character
 lc>b          #  If c>a, do this again
]              # Close macro definition
ddd sb         # DDDuplicate and store as `b'
96sa C2sc x    # Set lower and upper bounds (`a' and `c') for [a-z] and execute a copy of b
64sa 90sc x    # Set bounds for [A-Z] and execute a copy of b
47sa 57sc x    # Set bounds for [0-9] and execute a copy of b
95P            # Print underscore

Groovy, 36 characters


Sample run:

bash-4.3$ groovy -e 'print(("0".."z").grep(~/\w/).join())'

RETURN, 20 bytes

'_0 9

Try it here.

Creates a series of charcode ranges and outputs them.


𝔼𝕊𝕄𝕚𝕟, 10 chars / 13 bytes


Try it here (ES6 browsers only).

Translated roughly to ES6:


16-bit x86 machine code, 26 bytes

In hex:


Input: DI: pointer to an array of at least 63 bytes. Function outputs the sequence


without any termination, since its length is constant.


00: B0 30       mov    al,'0'
02: B9 0A 00    mov    cx,10
05: AA          stosb              ;*DI++=AL
06: 40          inc    ax          ;AL++
07: E2 FC       loop   _00000005   ;Print digits
09: B0 5F       mov    al,'_'      ;0x5f
0B: AA          stosb
0C: 40          inc    ax          ;AX=0x60
0D: B1 1A       mov    cl,26
0F: 50          push   ax
10: 40          inc    ax
11: AA          stosb
12: E2 FC       loop   _00000010   ;Print single-case letters
14: 58          pop    ax
15: 34 20       xor    al,020      ;Flip "case" bit
17: 7B F4       jnp    _0000000D   ;Repeat if "not parity", i.e. AX is back to 0x40
19: C3          ret

Common Lisp, 61 bytes

using formule for lowercase'ing from Practical Lisp, namely (format t "~(~a~)" "ExAMpleStRINg")

(format t"~a~:*~(~a~)0123456789_"'abcdefghijklmnopqrstuvwxyz)

Ideas for improvement are welcomed.


QBIC, 66 41 bytes

Had some coffee and re-read the specs:


This prints Aa\n all the way up to Zz\n, then the literal 0123456789_.

There are quite a lot of ways QBIC can approach this challenge if we want to print the string in the order provided.

By adding all the requested ranges to Z$ (Z$ gets printed implicitly), 71 bytes


Printing each range to screen, with ';` to prevent newlines, 67 bytes


Creates one range, printing only substrings, 70 bytes


Creating one range, but with modified start of that range, 69 bytes


But sadly the best at 66 bytes, just plainly printing this string:


q/kdb+, 35 bytes






Turns out I cannot read :) Q.an solves this in 5 bytes. I'll try to golf this new solution.

  • 1
    \$\begingroup\$ "Built-ins that contain 9 or more of these characters are disallowed." \$\endgroup\$ Jun 22 '17 at 23:34

RProgN 2, 9 bytes



•           # Push a space
 z          # Push the character 'z'
  R         # Create a stack of all characters from space to z.
   .        # Concatenate them into a single string.
    '\W'-   # Filter out everything that matches the regex '\W' (Non-alpha-numeric characters)
            # Implicit print

Try it online!


JavaScript (ES7), 60 57 bytes

Shame we can't return an array or set of the characters, I could've saved quite a few bytes there.

_=>[...new Set(Object.keys(this).join`4Z_`+2**29)].join``

Test it

The second line is sorted to make it easy to check all characters are included.

_=>[...new Set(Object.keys(this).join`4Z_`+2**29)].join``
<pre id=o></pre>


Fortran 95, 131 bytes

1 format(1A,25A,$)
2 format(10I1,$)

Just because every challenge is improved by a Fortran entry.

You'd think it would be easy to golf this down, but the required variable declarations get expensive very quickly so I think this is about as short as it's going to get.

If delimiters (\n) were allowed between characters in the output, this could be reduced to 83 bytes:


><>, 39 bytes


Try it online!

Fairly simple, prints _, loop prints a, A then increments and compares to Z. At the end it'll print 9-0.


Tcl, 68 bytes

puts abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_

Try it online!

tcl, 99

time {append s [incr i]} 9
set i 64;time {set s $s[format %c%c [incr i] [expr $i+32]]} 26
puts _0$s


tcl, 100

time {append s [incr i]} 9
time {set s $s[format %c%c [expr [incr i]+55] [expr $i+87]]} 26
puts _0$s



T-SQL, 70 bytes


Procedural solutions in SQL are just too long, due to the length of keywords. A couple of my best attempts:

76 Bytes, store the alphabet then UPPER it:

DECLARE @ CHAR(26)='abcdefghijklmnopqrstuvwxyz'PRINT'0123456789_'+@+UPPER(@)

100 Bytes, add all chars from 65-122, then strip out the ones I don't want:

DECLARE @ VARCHAR(99)='0123456789'A:SET @+=CHAR(LEN(@)+55)IF LEN(@)<68GOTO A
PRINT STUFF(@,37,6,'_')

MATLAB, 30 bytes


Very simple :)

Or, at the same cost:




Japt, 13 12 bytes

;C+B+9ô ¬+'_

Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.