How can I get a character in a string by index?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Getting a character from a string by index is a basic operation, but the exact syntax and edge cases depend on the language. The main ideas are the same everywhere: strings are ordered sequences, indexes usually start at 0, and out-of-range access must be handled carefully.
Zero-Based Indexing
Most mainstream languages use zero-based indexing. That means:
- index
0is the first character - index
1is the second character - index
len - 1is the last character
So in the string "hello":
- '
his at index0' - '
eis at index1' - '
ois at index4'
That indexing rule is simple, but off-by-one mistakes are still common.
Python Example
In Python, use square brackets.
Python also supports negative indexes, so -1 means the last character. If the index is out of range, Python raises IndexError.
JavaScript Example
In JavaScript, you can use bracket syntax or charAt.
If the index is out of range, text[index] returns undefined, while charAt returns an empty string. That difference matters when you write defensive code.
Java Example
In Java, use charAt.
If the index is invalid, Java throws StringIndexOutOfBoundsException.
Strings Versus Characters
It is worth noting that some languages have a distinct character type and some do not.
- Java returns a
char - Python returns a one-character string
- JavaScript returns a one-character string
That usually does not matter for simple printing, but it matters when you compare types or call character-specific APIs.
Bounds Checking Matters
Before accessing by index, you often need to confirm the index is valid.
Python example:
Java example:
This is especially important when the index comes from user input, parsing, or another algorithm.
Unicode Can Complicate the Story
For simple ASCII text, character-by-index usually behaves the way people expect. With full Unicode, things can get more subtle. Some visible symbols are made of multiple code units or code points, depending on the language and runtime representation.
That means "the third visible character" is not always the same as "index 2 in the string." For normal beginner-level cases, direct indexing is fine. For advanced text processing, you may need Unicode-aware libraries or grapheme-cluster handling.
Common Pitfalls
The most common mistake is forgetting that indexing usually starts at 0, not 1.
Another mistake is not handling out-of-range indexes, which leads to exceptions or unexpected values depending on the language.
A third pitfall is assuming every language returns the same type when you access a single character.
That detail matters in reusable libraries and APIs.
Summary
- Most languages use zero-based indexing for strings.
- Python uses
text[index], JavaScript supportstext[index]andcharAt, and Java usescharAt. - Always handle out-of-range indexes carefully.
- Returned types differ by language: some return a character type and others return a one-character string.
- For complex Unicode text, visible characters and index positions are not always the same thing.
Related reading
- How can I get a list of user accounts using the command line in MySQL?
- How can I get a list of user accounts using the command line in MySQL?
- How can I get column names from a table in SQL Server?
- How can I get the size of a MySQL database?
- How can I get the size of a MySQL database?
- How can I get the sizes of the tables of a MySQL database?
- How can I get the sizes of the tables of a MySQL database?
- How can I get the SQL of a PreparedStatement?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.