How to get the first character of a string in Python? [5 Methods]

Do you want to learn how to get the first character of a string in Python? If so, then you are in the right place.

In this Python tutorial, I will describe the step-by-step explanation of various methods to get the first character of a string with illustrative examples.

Here, I will explain the following methods to get the first character of a string in Python: using the conditional statement, string slicing, using the str() function, the startwith() function, and regular expression.

Now, let’s look at each of these methods in detail below.

Python program to get the first character of a string using 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.

This is the complete Python program to get the first character of a string using a conditional statement.

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

Refer to the screenshot below it will display the first character of a string using a conditional statement in Python.

Get the first characters of a string in Python

Get the first character of a string in Python using string slicing

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

READ:  Python file Does Not Exist Exception[5 methods]

This is the Python complete program to get the first character of a string using string slicing in Python.

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

The screenshot below shows the Python program’s output to get the first character of a string in Python using string slicing.

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

How to get the first character of a string in Python using the startwith() method

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

Here is the full code of Python to get the first character of a string using the startwith() method.

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 see the output of the Python program to get the first character of a string using the startwith() method shown below.

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

Python program to get the first character of a string using 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. In Python, regular expressions provide a powerful and flexible way to search, match, and manipulate strings based on specific patterns.

The source code of Python to get the first character of a string using the regular expression.

import re

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

We 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

Refer to the screenshot below to see the output of the Python program to get the first character of a string using the Regular Expression.

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

Conclusion

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.

READ:  How to get string values from list in Python

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: