In Python, you can use array-like indexing or slicing to get the first character of a string.
In this example,
str
with a value of "Hello World"
.0
on the string str
or slicing with str[0:1]
to get the first character.str = "Hello World"
firstChar = str[0]
print('First character:', firstChar)
First character: H
In this example,
str
with a value of "apple"
.0
on the string str
or slicing with str[0:1]
to get the first character.str = "apple"
firstChar = str[0]
print('First character:', firstChar)
First character: a
In this tutorial, we learned How to get First Character in String in Python language with well detailed examples.