Python Program to Get a Character From the Given String


In Python we can get a character from the given string using the indexing operator ‘[ ]’, using slices, and using indices separated by a colon. By passing the index of the character we want to access to the index operator we can easily get the character from the string. In this article, we will see how we can access the character of the string using the index operator.

Using [ ] Operator

Syntax

string[index]

Here string is the given string from which we want to access a specific character. The index is the index of the character in the string.

Example 1

In the below example, we initialize a string “Hello World” and get the character at position 0 using the index attribute.

string = "Hello World"
print(string[0])

Output

H

Example 2

Any character of the string can be accessed using the index of the string. In the below example, we get the third character of the string using the index as 2.

string = "Hello World"
print(string[-1])

Output

d

Example 3

The last index of the string can also be accessed using the negative index. In the below example, we created a string “Hello World”. We can access the last character of the string by passing the index as -1 as an index to the indexing operator ([ ]).

string = "Hello World"
print(string[10])

Output

d

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.

Example

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!

Using Indices Separated by a Colon

Example 1

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 2

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

Conclusion

In this article, we discussed how we can access any character of the string by using the index operator ([ ]) in a simple way. The index operator is used in almost all programming languages.

Updated on: 17-Apr-2023

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements