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 : SYou can refer to the screenshot below to see the output.

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: KYou can refer to the screenshot below to see the output.

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.

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: JYou can refer to the screenshot below to see the output.

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:
- Reverse a List in Python
- Convert a List to a String in Python
- Remove the Last Element from a List in Python
- Flatten a List of Lists in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.