Get First Character from String

To get the first character from a string in Python, access the character from string using square brackets and pass the index 0.

The following expression returns the first character from the string myString.

</>
Copy
myString[0]

Example

In the following program, we take a string in name variable, and get the first character using square brackets notation and 0 index.

main.py

</>
Copy
name = 'banana'

firstChar = name[0]
print(f'First Character : {firstChar}')

Output

First Character : b

Conclusion

In this Python Tutorial, we learned how to get the first character from a string in Python using square brackets notation.