Python program to get characters from the string using the index


In Python, we can use various methods like using square brackets, slicing, and using indices to get characters from the string with the help of their index. A string is a sequence of characters with each character being assigned a unique index. The index specifies the position of the character in the string. The index of the first character starts with 0 and the index of the last character in the string is the length of the string minus one. We can use the index value of the character to access a particular character in the string.

Accessing Characters From the String Using the Index

The character of the string can be accessed using the index of the string in a square bracket [ index ]. If we want to access the last character of the string then we can pass the [length of the string- 1] inside the square bracket.

Example: Using Square Bracket and Index

In the below example, we consider the string “Hello, World!” and stored it in a variable my_string. To access the first character of the string we can use the index 0 as follows.

my_string = "Hello, World!"
first_character = my_string[0]
print(first_character) 

Output

H

Example: Using Negative Indices

We can also access the characters of the string from the end using a negative index starting from -1 for the last character in the string.

my_string = "Hello, World!"
last_character = my_string[-1]
print(last_character)

Output

!

Example: Using Indices Separated by a Colon

We can access multiple characters from a string by using a range of indices. We provide the starting and ending index separated by a colon. The starting index character is included in the multiple characters but the ending character is not included in the multiple strings we are trying to access. We can access the first three characters of the string “Hello, World!” as follows −

my_string = "Hello, World!"
first_three_characters = my_string[0:3]
print(first_three_characters) 

Output

Hel

Example

To get the characters from 6 to 11 index we use the range 6:12.

my_string = "Hello, World!"
characters_6_to_11 = my_string[7:12]
print(characters_6_to_11)  # Output: World

Output

World

Example: Using Slices

Slices are used to get multiple characters from the string/ Slices are similar to the range but are more precise. The slice method takes the start index, end index, and the step size i.e ‘start:end:step’ to get multiple characters from the string. Step size indicates the number of jumps to get the character from the string.

To get every alternate character in the string we can use step 2. In order to indicate the slice method to scan the string from the first character to the last character in the string we can leave the start index and the end index blank.

my_string = "Hello, World!"
every_other_character = my_string[::2]
print(every_other_character) 

Output

Hlo ol!

Example: Using Slices

To get the character from index 3 to index 9 with step 2 we use the slice with parameter as 3:10:2.

my_string = "Hello, World!"
characters_3_to_9_every_other = my_string[3:10:2]
print(characters_3_to_9_every_other)

Output

l,Wr

Resolving IndexError

If we try to access an index outside the range of the string will result in IndexError. If we try to access the 20th index of a string of length 12 then it will give an IndexError. To handle index errors we can use the try-except block as follows −

Example

my_string = "Hello, World!"
try:
   character_at_index_20 = my_string[20]
except IndexError:
   print("Index out of range")

Output

Index out of range

Conclusion

In this article, we discussed how to get in the character of the string using the index. We can access single as well as multiple characters of the string. To access multiple characters we can use a range index with a start and end index separated by a colon. Also, we can use the slice function with start index, end index, and step size to access multiple characters of the string.

Updated on: 17-Apr-2023

369 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements