How to get the first 2 letters of a string in Python?

It is as simple as string[:2]. A function can be easily written to do it, if you need.

Even this, is as simple as

def first2(s):
    return s[:2]

In general, you can get the characters of a string from i until j with string[i:j].

string[:2] is shorthand for string[0:2]. This works for lists as well.

Learn about Python's slice notation at the official tutorial


t = "your string"

Play with the first N characters of a string with

def firstN(s, n=2):
    return s[:n]

which is by default equivalent to

t[:2]