How to Get a Substring From a String in Python

In Python, Strings are arrays of bytes representing Unicode characters. It's possible to access individual characters of a string by using array-like indexing . Like an array data type that has items that correspond to an index number , each of a string's characters also correspond to an index number, starting with the index number 0.

Python substring

Python has no substring methods like substring() or substr(). Instead, we use slice syntax to get parts of existing strings. Python slicing is a computationally fast way to methodically access parts of your data. The colons (:) in subscript notation make slice notation - which has the arguments, start, stop and step . It follows this template:
  1. Parameters are enclosed in the square brackets.
  2. Parameters are separated by colon.
string[start: end: step]
  1. start - Starting index of string, Default is 0.
  2. end - End index of string which is not inclusive .
  3. step - An integer number specifying the step of the slicing. Default is 1.

How Indexing Works

You can make any of these positive or negative numbers for indexing. The meaning of the positive numbers is straightforward (from start), but for negative numbers, just like indexes in Python, you count backwards from the end for the start and stop, and for the step (optional), you simply decrement your index.
Python string substring

Python substring examples

Get all characters of the string
str="python substring" print(str[0:16]) //Get all characters of the string
output
python substring

End index only

str="python substring" print(str[:16]) //Get all characters of the string
output
python substring

Start index only


How to extract a substring from inside a string in Python?

No Indexes specified

str="python substring" print(str[:]) //Get all characters of the string
output
python substring

How to get the first part from a string

str="python substring" print(str[0:6])
output
python

Get last part from a string


python slice

Get first character from a string

str="python substring" print(str[0:1]) //first character if the string
output
p

Get last character from a string

str="python substring" print(str[15:16]) //last character of the string
output
g

Substring from right side of the string

You may also get the substring from the right side of the string. A negative index means that you start counting from the end of the string(from right to left) instead of the beginning. Index [-1] represents the last character of the string, [-2] represents the second to last character.
str="python substring" print(str[-1:]) //get the last character of the string
output
g

How to get the last 9 character from Python string

str="python substring" print(str[-9:])
output
substring

Get a substring which contains all characters except the last 10 characters

str="python substring" print(str[0:-10])
output
python

Step in Python substring

The third parameter specifies the step , which refers to how many characters to move forward after the first character is retrieved from the string. Python defaults to the step of 1, so that every character between two index numbers is retrieved.
str="python substring" print(str[0:16]) //Get all the characters from the string

Add step 1

str="python substring" print(str[0:16:1])
output
python substring
Here, we get the same results by including a third parameter with a step of 1.

Get every other character from a python string

Change step to 2

str="python substring" print(str[0:16:2])
output
pto usrn
Here you can see, you get the every other character from python string .

Change step to 3

str="python substring" print(str[0:16:3])
output
ph brg

Reverse a string in Python


how to reverse a string in python