The Python string data type is a sequence made up of one or more individual characters that could consist of letters, numbers, whitespace characters, or symbols. As the string is a sequence, it can be accessed in the same ways that other sequence-based data types are, through indexing and slicing.
Indexing means referring to an element of an iterable by its position within the iterable. Each of a string’s characters corresponds to an index number and each character can be accessed using their index number.
We can access characters in a String in Two ways :
- Accessing Characters by Positive Index Number
- Accessing Characters by Negative Index Number
1. Accessing Characters by Positive Index Number:
In this type of Indexing, we pass a Positive index(which we want to access) in square brackets. The index number start from index number 0 (which denotes the first character of a string).
Example 1 (Positive Indexing) :
G f G
2. Accessing Characters by Negative Index Number :
In this type of Indexing, we pass the Negative index(which we want to access) in square brackets. Here the index number starts from index number -1 (which denotes the last character of a string).
Example 2 (Negative Indexing) :
! e f
Slicing in Python is a feature that enables accessing parts of sequence. In slicing string, we create a substring, which is essentially a string that exists within another string. We use slicing when we require a part of string and not the complete string.
string[start : end : step] start : We provide the starting index. end : We provide the end index(this is not included in substring). step : It is an optional argument that determines the increment between each index for slicing.
Example 1 :
Gee ek !ke o
Example 2 :
Original String :- Geeks for Geeks! Reverse String :- !skeeG rof skeeG
- Python | Pandas Series.str.slice()
- Python | Incremental slice partition in list
- Python | Slice String from Tuple ranges
- Use of slice() in Python
- Python slice() function
- Python - Get sum of last K list items using slice
- Python - Slice from Last Occurrence of K
- Python - Slice till K dictionary value lists
- Python - K elements Reversed Slice
- Dunn index and DB index - Cluster Validity indices | Set 1
- Python | Get first index values in tuple of strings
- Python | Kth index character similar Strings
- Python - Extract indices of Present, Non Index matching Strings
- Python - Remove similar index elements in Strings
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- Python | Index of Non-Zero elements in Python list
- Python String index() and its applications
- Python | Accessing index and value in list
- Python | Separate odd and even index elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.