string manipulation
indexing
programming
character retrieval
coding basics

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.

Practice system design

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 0 is the first character
  • index 1 is the second character
  • index len - 1 is the last character

So in the string "hello":

  • 'h is at index 0'
  • 'e is at index 1'
  • 'o is at index 4'

That indexing rule is simple, but off-by-one mistakes are still common.

Python Example

In Python, use square brackets.

python
text = "hello"
print(text[1])
print(text[-1])
text
e
o

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.

javascript
const text = "hello";
console.log(text[1]);
console.log(text.charAt(1));
text
e
e

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.

java
1public class Main {
2    public static void main(String[] args) {
3        String text = "hello";
4        System.out.println(text.charAt(1));
5    }
6}
text
e

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:

python
1text = "hello"
2index = 3
3
4if 0 <= index < len(text):
5    print(text[index])

Java example:

java
1String text = "hello";
2int index = 3;
3
4if (index >= 0 && index < text.length()) {
5    System.out.println(text.charAt(index));
6}

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 supports text[index] and charAt, and Java uses charAt.
  • 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.