Python Extract First Character: Easy Guide

To extract the first character of a string, you can use the indexing operation on the string. In Python, the index of a string starts at 0, so the index of the first character is 0. Here is an example code:

s = "Hello, World!"
first_char = s[0]
print(first_char)

The output results in: H

In the above code, we defined a string s, then used the index operation s[0] to extract the first character, and assigned it to the variable first_char. Finally, we printed out first_char.

bannerAds