How to get first character of String in Python?

Python – Get First Character of String

To get the first character of the given string in Python, we can use string indexing using square brackets.

Syntax

The syntax to get the first character of the string x is

x[0]

Example

In the following program, we take a string value in variable name, and get the first character of the string.

Python Program

name = 'mango'

if len(name) > 0:
    firstChar = name[0]
    print(f'First character : {firstChar}')
else:
    print('The string is empty. Please check.')
Run Code

Output

First character : m

Summary

In this tutorial of Python Examples, we learned how to get the first character of a string, with examples.

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us