Getting the First Character of a String in Python

In this article, we will show you how to get the first character of a string in Python using various methods. We will also provide examples and explain why each method is useful.

Author: Jeremy Morgan
Published: December 11, 2023


There are several ways to get the first character of a string in Python. Here are some of the most common methods:

Method 1: Using the index operator

The simplest way to get the first character of a string is to use the index operator ([]). The index operator allows you to access a specific element of a sequence (such as a string) by its position. To get the first character of a string using the index operator, simply write:

my_string = "Hello, World!"
first_char = my_string[0]
print(first_char) 

Here’s the output:

“How to get the first character of a string in Python”

In this example, we create a new variable first_char and assign it the first character of the string my_string using the index operator. The number inside the brackets indicates the position of the element in the sequence that you want to access. In this case, 0 is the first position, so we get the first character of the string.

Method 2: Using the built-in function chr()

Another way to get the first character of a string in Python is to use the built-in function chr(). The chr() function takes an integer as its argument and returns the corresponding Unicode code point. To get the first character of a string using chr(), you can pass it the ASCII value of the character.

my_string = "Hello, World!"
first_char = chr(ord(my_string[0]))
print(first_char) 

Here is the output:

“How to get the first character of a string in Python”

In this example, we first get the ASCII value of the first character of the string using ord(). We then pass this value to the chr() function, which returns the corresponding Unicode code point.

Method 3: Using slicing

You can also use slicing to get the first character of a string in Python. Slicing allows you to extract a subset of elements from a sequence (such as a string). To get the first character of a string using slicing, simply write:

my_string = "Hello, World!"
first_char = my_string[:1]
print(first_char)  # Output: H

Here is the output:

“How to get the first character of a string in Python”

Want to get the first three characters of a string? Easy, you can specify that too:

my_string = "Hello, World!"
first_char = my_string[:3]
print(first_char)

Here’s the output:

“How to get the first character of a string in Python”

In this example, we use slicing to extract the first element of the string. The colon : indicates that we want to include all elements from the start of the sequence up to (but not including) the position specified after the colon. In this case, we specify a slice from the beginning of the string up to (but not including) the second character. This gives us the first character of the string.

Advantages and Disadvantages of Each Method

Each method has its own advantages and disadvantages. Here are some pros and cons of each method:

Method 1: Using the index operator

Advantages:

  • Simple to implement
  • Fast execution time

Disadvantages:

  • Only works for strings
  • Does not work for other sequence types (such as lists or tuples)

Method 2: Using the built-in function chr()

Advantages:

  • Works for any Unicode code point
  • Can be used to convert integers to characters

Disadvantages:

  • Requires knowledge of ASCII values and Unicode code points
  • Slower execution time compared to method 1

Method 3: Using slicing

Advantages:

  • Works for any sequence type (strings, lists, tuples)
  • Can be used to extract a subset of elements from a sequence

Disadvantages:

  • More complex to implement than method 1
  • Slower execution time compared to method 1 and method 2

There are several ways to get the first character of a string in Python. There are advantages and disadvantages to each method, so choose the one that works best for you. Understanding how to extract the first character of a string is an essential Python programming skill for beginners and experts alike.