A string is a collection of characters.
A character is simply a representation of something. The English language, for example, has 26 characters.
Computers work with numbers rather than characters (binary). Despite the fact that you see characters on your screen, they are stored and manipulated internally as a series of 0s and 1s.
The process of converting a character to a number is known as encoding, and the process of converting a number to a character is known as decoding. Two of the most common encodings are ASCII and Unicode.
A string in Python is a sequence of Unicode characters. Unicode was created in order to include every character in every language and to bring encoding uniformity. Python Unicode is capable of teaching you everything you need to know about Unicode.
Examples:
Input:
string="Hello this is BTechGeeks" index=3
Output:
l
Retrieve characters in string by index
In this article, we will look at how to access characters in a string using an index.
- Accessing characters in a string by index | indexof
- Using a negative index to access string elements
- Using String Slicing :
- Accessing Out Of range character in given string using Exceptional Handling:
>1)Accessing characters in a string by index | indexof
Python uses a zero-based indexing system for strings: the first character has index 0, the next has index 1, and so on.
The length of the string – 1 will be the index of the last character.
Suppose we want to access 3rd index in input string
We can implement it as:
# Given string string = "Hello this is BtechGeeks" # Given index index = 3 character = string[index] # printing the character at given index print(character)
Output:
l
>2)Using a negative index to access string elements
Negative numbers may be used to specify string indices, in which case indexing is performed from the beginning of the string backward:
The last character is represented by string[-1]
The second-to-last character by string[-2], and so on.
If the length of the string is n, string[-n] returns the first character of the string.
Below is the implementation:
# Given string string = "Hello this is BtechGeeks" # given index from end index = 3 # printing last two characters of string print("Last character in the given string : ", string[-1]) print("Second Last character in given string : ", string[-2]) # printing n th character from end print(str(index)+" character from end in given string :", string[-index])
Output:
Last character in the given string : s Second Last character in given string : k 3 character from end in given string : e
>3)Using String Slicing :
# Given string string = "Hello this is BtechGeeks" # given start index and end index startindex = 14 endindex = 19 # using slicing print(string[startindex:endindex])
Output:
Btech
>4)Accessing Out Of range character in given string using Exceptional Handling:
IndexError will be thrown if an element in a string is accessed that is outside of its range, i.e. greater than its length. As a result, we should always check the size of an element before accessing it by index, i.e.
To handle the error we use exceptional handling in python
Below is the implementation:
# Given string string = "Hello this is BtechGeeks" # given index index = 50 # using try and except try: # if the given index is less than string length then it will print print(string[index]) # if index is out of range then it will print out of range except: print("Index out of range")
Output:
Index out of range
Related Programs:
- python how to remove characters from a string by index
- python how to get first n characters in a string
- python how to replace single or multiple characters in a string
- python how to get last n characters in a string
- python pandas how to drop rows in dataframe by index labels
- how to access element by index in a set c
- python program to remove the characters of odd index values in a string