Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

For a non-empty Python string s, use s[0]. Python sequences start at index 0, and this returns a one-character str:

s = "Python"
first = s[0]  # "P"

If the string might be empty and you want an empty string rather than an error, use s[:1].

Get the first character with indexing

Python strings are zero-indexed, so the first element is at position 0. For any non-empty string, s[0] returns a string of length one. The Python built-in types reference explains that Python has no separate character type: indexing a string produces a string of length one.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
word = "Python"
first = word[0]

print(first)  # P

Handle an empty string

Indexing requires an element at the requested position. If s is "", then s[0] raises IndexError.

Expression For a non-empty string For an empty string Use when
s[0] Returns the first character as a one-character str. Raises IndexError. An empty input is invalid or should be handled explicitly.
s[:1] Returns a one-character str. Returns "". An empty result is acceptable when the input is empty.

For example, use a slice when an empty result is a valid outcome:

first_or_empty = s[:1]

If an empty string signals invalid input, check for it and raise or handle an application-specific error instead of silently returning "". Python’s tutorial covers zero-based indexing and slice defaults.

Understand what Python means by a character

A Python str is an immutable sequence of Unicode code points. Therefore, s[0] returns one code point, not necessarily one complete symbol as a person sees it on screen. Some displayed symbols consist of multiple code points. If an interface must split text into user-perceived characters, use grapheme-aware Unicode processing rather than assuming each s[i] is a whole visual character.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Decode bytes before treating data as text

If the value is bytes rather than str, indexing it does not return a text character. Decode the bytes with the correct encoding first, then index the resulting string:

text = data.decode("utf-8")
first = text[0]

Do not use str(data) as a substitute for decoding: without encoding arguments, it produces an informal representation of the bytes object, not decoded text.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.