To get a character at a specific index in a string in Python, you can use array-like indexing or the []
operator.
In this example,
str
with a value of "Hello World"
.[]
operator on the string str
.str = "Hello World"
char_at_index_4 = str[4]
print('Character at index 4:', char_at_index_4)
Character at index 4: o
In this example,
str
with a value of "Python"
.[]
operator on the string str
.str = "Python"
char_at_index_1 = str[1]
print('Character at index 1:', char_at_index_1)
Character at index 1: y
In this tutorial, we learned How to get Character at Specific Index in String in Python language with well detailed examples.