Skip to content

Python Substring | Slicing & Splitting String | Examples

  • by

Slicing or Splitting a string in Python and get substring is easy. You can get python substring by using a split() function or do with Indexing. Indexing is a very important concept not only with strings but with all the data types, such as lists, tuples, and dictionaries.

When we create a string or sentence in python it’s every character associated with the index, which starts from 0.

Python Substring | Slicing & Splitting String | Examples

Python every string that you created it assigns a number to each of the items of your string, so it starts from 0.

Let me create a “Hi Python !” Sting in python, so “H “will be assigned an index of 0 and then a 1 for “I“, 2 for space, 3 for “P”, five, six, seven, eight until the end.

Python Substring string indexing

This allows you to extract certain parts from a string and there is a certain notation to do that you’d want to access the string.

Syntax

Here is simple Python Substring syntax.

string[start:end]: Get all characters from index start to end-1

string[:end]: Get all characters from the beginning of the string to end-1

string[start:]: Get all characters from index start to the end of the string

Python Substring Example 

Let’s do get the first letter of the string, variable str holding the string, then you have to use square brackets so an opening square bracket and inside those square brackets, you pass the index of the item that you want to extract or Slicing string or Splitting string.

Get the First Character Example 

Pass the 0 in square bracket [ ]  -> [0]

str = 'Hi Python !'

print(str[0])

Output: H

Get the Last Character Example 

Get the length of the string (sentence) and subtract by – 1, because indexing starts from 0. So length will be.length -1 Here is an example of it :

Or you can pass -1 in an index, it’s a minus indexing.

str = 'Hi Python !'
l = len(str)

print(str[l - 1])
print(str[-1])

Output: !

Substring from String Example

Let’s get the Hi, for that you have to pass the 0 to 2 in square brackets. Like this.str[0:2]

It doesn’t return an item with index 2. That’s because splitting in Python, Python is upper bound exclusive, which means that the upper bounds of the split here are not included in the output.

If you want, include passing the index.

If you pass 3 there, that would include the wide space as well and similarly, you can pass like one. You can also say slicing of string in python.

str = 'Hi Python, Tutorial !'

print(str[0:2])
print(str[11:19])

Output: Hi
Tutorial

Get the first 5 characters of a string

str = 'Hi Python, Tutorial'

print(str[:5])

Output: Hi Py

Get the last 5 characters of a string

str = 'Hi Python, Tutorial'

print(str[5:])

Output: thon, Tutorial

Minus indexing python substring 

In python, there is also support for minus indexing. If you pass the -1 index then the last character will get. If minus – 2, -3, ….then 2nd last, 3rd last …etc.

str = 'Hi Python, Tutorial'

print(str[-1])
print(str[-2])

Output: l

a

You can use minus indexing to get substring or splitting or slice of the sentence.

Get every other character from a string

str = 'Hi Python'

print(str[::3])

Output: HPh

You can do the same for 2 or any other character as per requirements. So that’s enough example to learn to slice a python string.

Do comment if you have another example or doubts or suggestions on this tutorial.

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Python Substring Examples are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading