Skip to content
Related Articles

Related Articles

How To Index and Slice Strings in Python?
  • Last Updated : 24 Jul, 2020

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

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 :

  1. Accessing Characters by Positive Index Number
  2. 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).

Indexing in Python


Example 1 (Positive Indexing) :




# declaring the string
str = "Geeks for Geeks !"
  
# accessing the character of str at 0th index
print(str[0])
  
# accessing the character of str at 6th index
print(str[6])
  
# accessing the character of str at 10th index
print(str[10])

Output :



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) :




# declaring the string
str = "Geeks for Geeks !"
  
# accessing the character of str at last index
print(str[-1])
  
# accessing the character of str at 5th index from the last
print(str[-5])
  
# accessing the character of str at 10th index from the last
print(str[-10])

Output :

!
e
f

Slicing

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.
Syntax :

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 :




# declaring the string
str ="Geeks for Geeks !"
  
# slicing using indexing sequence 
print(str[: 3]) 
print(str[1 : 5 : 2]) 
print(str[-1 : -12 : -2])

Output :

Gee
ek
!ke o

Example 2 :




# declaring the string
str ="Geeks for Geeks !"
  
print("Original String :-")
print(str)
  
# reversing the string using slicing
print("Reverse String :-"
print(str[: : -1]) 

Output :

Original String :-
Geeks for Geeks!
Reverse String :-
!skeeG rof skeeG

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :