How to Get the First Character of a String in Python

I was working on a project where I needed to quickly check the first letter of customer IDs stored in a database.

At first, I thought there might be only one way to do this. But after experimenting, I realized Python offers several simple methods to extract the first character of a string.

In this tutorial, I’ll show you five easy methods I personally use. Each example is easy, and you can try them instantly in your Python environment.

Method 1: Use a Conditional Statement

In Python, If-Else is a part of a conditional statement. It will control the direction of the flow in the Python program for execution.

name = 'San Diego'

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

Output:

First character : S

You can refer to the screenshot below to see the output.

Get the first characters of a string in Python

This method uses a simple if-else condition to safely get the first character of a string while handling empty strings.

Method 2: Use String Slicing

String slicing in Python is used for obtaining a substring from the given string by slicing it from start to end.

Motivational_quote = "Keep Up The Good Work"
get_first_char = Motivational_quote [:1]
print("First character of the given string is:",get_first_char)

This is used to extract the first character (index 0) of the string. The extracted character is then assigned to the variable get_first_char.

get_first_char = Motivational_quote [:1]

Output:

First character of the given string is: K

You can refer to the screenshot below to see the output.

Python program get the first character of a string using string slicing

This method uses string slicing to extract the first character of a string efficiently and safely.

Method 4: Use the startwith() Method

This is another way to get the first character of a string using the startwith() method in Python. It returns True if the string starts with the specified value, otherwise False.

Top_Car = "Porsche Cayenne"
if Top_Car.startswith('P'):
    print("The string starts with 'P'")
else:
    print("The string does not start with 'P'")

Output:

The string starts with 'P'

You can refer to the screenshot below to see the output.

Get the first character of a string using the startswith function in Python

This method checks if a string starts with a specific character using startswith(), returning True or False. It’s useful for quick validations.

Method 5: Use the Regular Expression

If you are familiar with regular expressions, you can also use them to extract the first character of a string in Python. Python regular expressions provide a powerful and flexible way to search, match, and manipulate strings based on specific patterns.

import re

text_input = "John Smith"
first_character = re.match(r'(.)', text_input).group(1)
print("The first character from first character:",first_character)

I have used the re.match() function to match the pattern from the input data, and then we are using the group() function in Python to retrieve the portion of the string that matches the pattern within the first capturing group.

first_character = re.match(r'(.)', text_input).group(1)

Output:

The first character from first character: J

You can refer to the screenshot below to see the output.

How to get the first character of a string in Python using the Regular Expression

This method uses re.match() with a regex pattern to extract and return the first character of a string efficiently.

We explored various methods with descriptive examples to get the first character of a string in Python, like conditional statement, string slicing, startwith(), and regular expression.

I hope you find this article helpful, as I have explained the different methods with illustrative examples to get the first character of a string in Python.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.