In this article, we will discuss how to fetch/access the first N characters of a string in python. This N can be 1 or 3 etc.

Python string is a sequence of characters and each character in it has an index number associated with it. For example, we have a string variable sample_str that contains a string i.e.

Each character in this string has a sequence number, and it starts with 0 i.e.

‘H’ has index 0
‘e’ has index 1
‘l’ has index 2
‘l’ has index 3
‘o’ has index 4
‘ ‘ has index 5
‘W’ has index 6
‘o’ has index 7
‘r’ has index 8
‘l’ has index 9
‘d’ has index 10
‘!’ has index 11
‘!’ has index 12

In python String provides an [] operator to access any character in the string by index position. We need to pass the index position in the square brackets, and it will return the character at that index. This index position can be a positive or negative int value.

Like, sample_str[i] will return a character at index i-th index position. Let’s use it.

Get the first character of a string in python

As indexing of characters in a string starts from 0, So to get the first character of a string pass the index position 0 in the [] operator i.e.

Output:

It returned a copy of the first character in the string. You can use it to check its content or print it etc.

In the above example, we fetched the first character or the string, but what if we want more like, get the first three characters of a string or first four, etc. Basically we want to access a substring of given length from the start of the string. How to do that?

Get first N characters in a string

In python, apart from index position, subscript operator, i.e. [] can accept a range too i.e.

It returns a slice the string, i.e. a substring. If we want to get more than one character out of a string i.e. fetch a substring, then we can pass these range elements in [] operator,

  • start_index_pos: Index position, from where it will start fetching the characters, the default value is 0
  • end_index_pos: Index position till which it will fetch the characters from string, default value is the end of string
  • step_size: Interval between each character, default value is 1.

To get the first N characters of the string, we need to pass start_index_pos as 0 and end_index_pos  as N i.e.

The value of step_size will be default i.e. 0. It will slice the string from 0th index to n-1-th index and returns a substring with first N characters of the given string.

Let’s use this,

Get first three characters of a string in python

Output:

We sliced the string from 0 the index position to (3 -1) index position and we got a substring containing the first three characters of the string.

Get first four characters of a string in python

Output:

We sliced the string from 0 the index position to (4 -1) index position, and we got a substring containing the first four characters of the string.

IndexError: string index out of range

While using [] operator, we need to be careful about out of range error i.e. if we try to access the index position in a string that doesn’t exist, like a position that is larger than the size of the string, then it will give IndexError like this,

It will give the error,

Because we accessed index position 20 that doesn’t exist, it is larger than the size of the string. Therefore, we should always check for the size of string before accessing the character based on the index position. Like this,

Output:

The complete example is as follows,

Output:

Python Resources

C++11 / C++14 Resources

Design Patterns Resources

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,