How to Index and Slice Strings in Python? Last Updated : 31 Dec, 2024 Comments Improve Suggest changes Like Article Like Report In Python, indexing and slicing are techniques used to access specific characters or parts of a string. Indexing means referring to an element of an iterable by its position whereas slicing is a feature that enables accessing parts of the sequence.Table of ContentIndexing Strings in PythonAccessing by Positive Index NumberAccessing by Negative Index Number Slicing Strings in PythonIndexing Strings in PythonString Indexing allows us to access individual characters in a string. Python treats strings like lists where each character is assigned an index, starting from 0. We can access characters in a String in Two ways :Accessing Characters by Positive Index NumberAccessing Characters by Negative Index NumberIndexing in PythonAccessing by Positive Index NumberIn this type of Indexing we pass a Positive index (which we want to access) in square brackets. The index number starts from index number 0 (which denotes the first character of a string). python # declaring the string s = "Geeks for Geeks !" # accessing the character of str at 0th index print(s[0]) # accessing the character of str at 6th index print(s[6]) # accessing the character of str at 10th index print(s[10]) OutputG f GAccessing 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) : python # declaring the string s = "Geeks for Geeks !" # accessing the character of str at last index print(s[-1]) # accessing the character of str at 5th index from the last print(s[-5]) # accessing the character of str at 10th index from the last print(s[-10]) Output! e oSlicing Strings in PythonString Slicing allows us to extract a part of the string. We can specify a start index, end index, and step size. The general format for slicing is: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. python # declaring the string s ="Geeks for Geeks !" # slicing using indexing sequence print(s[: 3]) print(s[1 : 5 : 2]) print(s[-1 : -12 : -2]) OutputGee ek !seGrf Comment More infoAdvertise with us Next Article How to Index and Slice Strings in Python? ashishguru9803 Follow Improve Article Tags : Python python-string Practice Tags : python Similar Reads How to Substring a String in Python A String is a collection of characters arranged in a particular order. A portion of a string is known as a substring. For instance, suppose we have the string "GeeksForGeeks". In that case, some of its substrings are "Geeks", "For", "eeks", and so on. This article will discuss how to substring a str 4 min read Split and Parse a string in Python In this article, we'll look at different ways to split and parse strings in Python. Let's understand this with the help of a basic example:Pythons = "geeks,for,geeks" # Split the string by commas res = s.split(',') # Parse the list and print each element for item in res: print(item)Outputgeeks for g 2 min read Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is 2 min read String Slicing in Python String slicing in Python is a way to get specific parts of a string by using start, end and step values. Itâs especially useful for text manipulation and data parsing.Letâs take a quick example of string slicing:Pythons = "Hello, Python!" print(s[0:5])OutputHello Explanation: In this example, we use 4 min read Python String index() Method The index() method in Python is used to find the position of a specified substring within a given string. It is similar to the find() method but raises a ValueError if the substring is not found, while find() returns -1. This can be helpful when we want to ensure that the substring exists in the str 2 min read String Subsequence and Substring in Python Subsequence and Substring both are parts of the given String with some differences between them. Both of them are made using the characters in the given String only. The difference between them is that the Substring is the contiguous part of the string and the Subsequence is the non-contiguous part 5 min read Split a string on multiple delimiters in Python In this article, we will explore various methods to split a string on multiple delimiters in Python. The simplest approach is by using re.split().Using re.split()The re.split() function from the re module is the most straightforward way to split a string on multiple delimiters. It uses a regular exp 2 min read Python String rindex() Method Python String rindex() method returns the highest index of the substring inside the string if the substring is found. Otherwise, it raises ValueError.Let's understand with the help of an example:Pythons = 'geeks for geeks' res= s.rindex('geeks') print(res)Output10 Note: If start and end indexes are 2 min read How to Access Index using for Loop - Python When iterating through a list, string, or array in Python, it's often useful to access both the element and its index at the same time. Python offers several simple ways to achieve this within a for loop. In this article, we'll explore different methods to access indices while looping over a sequenc 2 min read Python | Pandas Index.get_slice_bound() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.get_slice_bound() function calculate slice bound that corresponds to give 2 min read Like