Printing A to Z in Various Languages – C#, Java, Python3, JS

What happened

I have been wanting to write this article for a long time. It all started when I encountered the problem LeetCode#387 . The English version of the article was completed in August 2022, and the Chinese translation came almost a year later – in July 2023.

Solution

The simplest way to achieve this task is by utilizing ASCII code.

Regarding ASCII code, let me briefly introduce it. It is essentially a coding scheme that originated from telegraph development. The main displayable range includes English letters, numbers, and some special characters.

The initial development of this encoding was for universal use, but it was primarily based on the Latin alphabet. Later, to address its limitations, Unicode was introduced, also known as the Universal Character Set (ASCII code is included in it).

Unicode assigns a unique code to each character, rather than defining a unique code for each glyph (i.e., an integer for each character).

The differences between the two lie not only in the range but also in the encoding method.

ASCII code uses 7 bits, meaning it can store 2 to the power of 7, which is 128 characters.

As for Unicode, it utilizes hexadecimal notation for storage.

For more detailed information, you can refer to the wiki, which lists the decimal numbers that each character will display.

After going through some details, let’s get back to the main point.

Since various programming languages process text using Unicode, and we know it is stored in hexadecimal, if you haven’t taken shortcuts in learning computer fundamentals, you should be aware that hexadecimal can be easily converted to decimal.

For example, let’s take the Chinese character “我” (pronounced as ““). Here, I’ll use C# as an example.

According to information found online – “我” U+6211 CJK Unified Ideograph-6211 Unicode Character (compart.com)

The Unicode code point for the character “我” is U+6211, which is equivalent to the decimal value 25105.

char a = '我';
// Print the Unicode code point of the character '我'
Console.Write((int)a);

// Print the character corresponding to the Unicode code point 25105
Console.WriteLine((char)25105);

This example can also validate this point.

And at this point, whether it’s Unicode or ASCII code, you should observe that the decimal values for A to Z are consecutive numbers.

Therefore, combining the above numerical characteristics, you can use a simple loop to generate the results~

C#

for (char c = 'a'; c <= 'z'; c++)
{
	Console.WriteLine(c);
}

That’s right; you can print out all the lowercase letters this way.

It’s important to note that the lowercase and uppercase letters are not consecutive.

The decimal values for uppercase A to Z range from 65 to 90.

The decimal values for lowercase a to z range from 97 to 122.

(This is a composition I cut and assembled from the information on the wiki. Apologies if it looks messy.)

So, if you want to print from lowercase ‘a’ to uppercase ‘Z’

for (char c = 'a'; c <= 'z'; c++)
{
	Console.WriteLine(c);
}
for (char c = 'A'; c <= 'Z'; c++)
{
	Console.WriteLine(c);
}

In the context of the following languages, we will specifically use lowercase ‘a’ to ‘z’ as an example (for any uncertainties, please refer to the explanation in C#).

Java

for (char c = 'a'; c <= 'z'; c++)
{
	//System.out.println((int)c);
	System.out.println(c);
}

Python3

for c in range(ord('a'), ord('z') + 1):
    # Print the Unicode decimal value
    # print(c)
    
    # Print the character using the Unicode decimal value
    print(chr(c))

In Python, it’s a bit different; you can’t directly loop over characters. In this case, you need to use the built-in function ord, which returns the Unicode decimal code for a given character.

Then, you can use range to iterate over the decimal codes.

Additionally, you can convert the decimal code back to the corresponding character using the built-in function chr.

(Alternatively, if you prefer to directly represent characters using numbers, that’s also possible. However, generally, who remembers those, haha!)

for c in range(97, 122 + 1):
	#print(c)    # Print the Unicode decimal value
	print(chr(c))

JavaScript

for (let c = 'A'.charCodeAt(0); c <= 'Z'.charCodeAt(0); c++) {
	//console.log(c); //# Print the Unicode decimal value
	console.log(String.fromCharCode(c))
}

(Similar to Python, you still need to use numbers to drive the loop.)


Conclusion

This time, I introduced four languages that I commonly use.

If there’s a need for additional languages or any areas that require further clarification, please feel free to leave a comment to let me know!

🧡You can support me by sharing my posts or clicking some ads, Thanks a lot

If you got any problem about this post, please feel free to ask me ~

Some Random notes

Leave a Reply

Your email address will not be published. Required fields are marked *