How to Get a Substring From a String in Python

In Python, strings are sequences of bytes that represent Unicode characters. Accessing individual characters within a string can be achieved using array-like indexing. Similar to array data types, where items are associated with an index number, each character in a string is also associated with an index number, starting from 0.

Python substring

Substring-specific methods like substring() or substr() are not available. Instead, we utilize slice syntax to extract portions of existing strings. Python's slicing is a highly efficient method for systematically accessing specific segments of data. The use of colons (:) in subscript notation allows us to employ slice notation, which consists of the start, stop, and step arguments, enabling flexible string manipulation. 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

Indexing for strings allows the use of positive or negative numbers. Positive numbers represent positions from the start of the string, while negative numbers count positions from the end of the string. When using negative numbers for start and stop, counting begins from the last character, and for the optional step argument, the index is decremented accordingly. This flexibility in indexing provides convenient ways to navigate and extract substrings from both ends of the string.

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
python substring

End index only

str="python substring" print(str[:16]) //Get all characters of the string
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
python substring

How to get the first part from a string

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

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

Get last character from a string

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

Substring from right side of the string

Python allows obtaining substrings from the right side of the string using negative indexing. A negative index indicates counting from the end of the string, from right to left, rather than from the beginning. For instance, index [-1] corresponds to the last character of the string, [-2] represents the second-to-last character, and so forth. This feature enables efficient access to characters starting from the end of the string.

str="python substring" print(str[-1:]) //get the last character of the string

How to get the last 9 character from Python string

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

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

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

Step in Python substring

The third parameter of slicing specifies the step, which determines the number of characters to move forward after the first character is retrieved from the string. By default, Python sets the step to 1, meaning that every character between the start and stop index numbers will be retrieved. However, you can modify the step value to skip characters and retrieve every nth character within the specified range. For example, using a step of 2 will extract every other character, and using a step of -1 will reverse the string. This flexibility in step allows for versatile string manipulation when slicing.

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])
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])
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])
ph brg

Reverse a string in Python

how to reverse a string in python


Python's substring slicing provides a flexible way to manipulate strings and efficiently access specific portions of data within a string. Keep in mind that slicing in Python does not modify the original string but returns a new substring as a result.