Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Get a Character From the Given String
In Python, you can access individual characters from a string using indexing with the [ ] operator. Python uses zero-based indexing, where the first character is at index 0. You can also use negative indexing to access characters from the end, and slicing to extract multiple characters.
Using [ ] Operator
Syntax
string[index]
Here string is the given string from which you want to access a specific character, and index is the position of the character (starting from 0).
Positive Indexing
Access characters using positive indices starting from 0 ?
string = "Hello World"
print("First character:", string[0])
print("Third character:", string[2])
print("Character at index 6:", string[6])
First character: H Third character: l Character at index 6: W
Negative Indexing
Access characters from the end using negative indices ?
string = "Hello World"
print("Last character:", string[-1])
print("Second last character:", string[-2])
print("Third last character:", string[-3])
Last character: d Second last character: l Third last character: r
Using Slicing
Slicing allows you to extract multiple characters using the syntax [start:end:step]. The start index is included, the end index is excluded, and step determines the interval between characters.
Extract Range of Characters
my_string = "Hello, World!"
first_three = my_string[0:3]
print("First three characters:", first_three)
middle_part = my_string[7:12]
print("Characters 7 to 11:", middle_part)
First three characters: Hel Characters 7 to 11: World
Using Step in Slicing
Extract every alternate character using step parameter ?
my_string = "Hello, World!"
every_other = my_string[::2]
print("Every alternate character:", every_other)
reverse_string = my_string[::-1]
print("Reversed string:", reverse_string)
Every alternate character: Hlo ol! Reversed string: !dlroW ,olleH
Practical Examples
Getting Multiple Characters at Specific Positions
text = "Programming"
positions = [0, 2, 4, 6]
characters = [text[i] for i in positions]
print("Characters at positions", positions, ":", characters)
Characters at positions [0, 2, 4, 6] : ['P', 'o', 'r', 'm']
Handling Index Errors
text = "Hello"
try:
print(text[10]) # This will cause an IndexError
except IndexError:
print("Index out of range!")
# Safe way to access characters
index = 10
if index < len(text):
print(text[index])
else:
print(f"Index {index} is out of range for string of length {len(text)}")
Index out of range! Index 10 is out of range for string of length 5
Comparison of Methods
| Method | Syntax | Purpose | Example |
|---|---|---|---|
| Positive Index | string[n] |
Get character from start |
"Hello"[0] ? 'H' |
| Negative Index | string[-n] |
Get character from end |
"Hello"[-1] ? 'o' |
| Slicing | string[start:end] |
Get substring |
"Hello"[1:3] ? 'el' |
| Step Slicing | string[::step] |
Get characters with interval |
"Hello"[::2] ? 'Hlo' |
Conclusion
Python provides flexible ways to access characters from strings using indexing and slicing. Use positive indices for accessing from the beginning, negative indices for accessing from the end, and slicing for extracting substrings or patterns.
