Like other programming languages, it’s possible to access individual characters of a string by using array-like indexing syntax. In this we can access each and every element of string through their index number and the indexing starts from 0. Python does index out of bound checking.
So, we can obtain the required character using syntax, string_name[index_position]:
- The positive index_position denotes the element from the starting(0) and the negative index shows the index from the end(-1).
Traceback (most recent call last): File "8a33ebbf716678c881331d75e0b85fe6.py", line 15, in <module> print x IndexError: string index out of range
e a o
To extract substring from the whole string then we use the syntax like
string_name[beginning: end : step]
- beginning represents the starting index of string
- end denotes the end index of string which is not inclusive
- steps denotes the distance between the two words.
Note: We can also slice the string using beginning and only and steps are optional.
lco oet Ge
This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Py-Facts - 10 interesting facts about Python
- Interesting facts about strings in Python | Set 1
- Interesting Facts about Macros and Preprocessors in C
- Interesting facts about null in Java
- Interesting Facts in C Programming
- Interesting facts about Fibonacci numbers
- Interesting facts about data-types and modifiers in C/C++
- Interesting facts about Increment and Decrement operators in Java
- Interesting facts about Array assignment in Java
- Interesting Facts about Ubuntu
- Interesting Facts about C++
- Interesting Facts about Linux
- Interesting Facts about C#
- Interesting Facts About Java
- Interesting Facts about PYGAME
- Python List Comprehension and Slicing
- String slicing in Python to check if a string can become empty by recursive deletion
- Basic Slicing and Advanced Indexing in NumPy Python
- Python Slicing | Reverse an array in groups of given size
- String slicing in Python to rotate a string
Improved By : nidhi_biet