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:- Parameters are enclosed in the square brackets.
- Parameters are separated by colon.
string[start: end: step]
- start - Starting index of string, Default is 0.
- end - End index of string which is not inclusive .
- 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 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

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

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

Related Topics
- Keywords in Python
- Python Operator - Types of Operators in Python
- Python Variables and Data Types
- Python Shallow and deep copy operations
- Python Datatype conversion
- Python Mathematical Function
- Basic String Operations in Python
- How to check if Python string contains another string
- Check if multiple strings exist in another string : Python
- Memory Management in Python
- Python Identity Operators
- What is a None value in Python?
- How to Install a Package in Python using PIP
- How to update/upgrade a package using pip?
- How to Uninstall a Package in Python using PIP
- How to call a system command from Python
- How to use f-string in Python
- Python Decorators (With Simple Examples)
- Python Timestamp Examples