Given a string, write a Python program to get the substring from given string using list slicing. Let’s try to get this using different examples.
What is substring?
A substring is a portion of a string. Python offers a variety of techniques for producing substrings, as well as for determining the index of a substring and more.
Syntax: myString[start:stop:step]
Parameter:
- start: It is the index of the list where slicing starts.
- stop: It is the index of the list where slicing ends.
- step: It allows you to select nth item within the range start to stop.
Example 1: In this example, we will see how to take a substring from the end or from starting of the string.
Python3
ini_string = 'xbzefdgstb'
print ( "initial_strings : " , ini_string)
sstring_strt = ini_string[: 2 ]
sstring_end = ini_string[ 3 :]
print ( "print resultant substring from start" , sstring_strt)
print ( "print resultant substring from end" , sstring_end)
|
Output:
initial_strings : xbzefdgstb
print resultant substring from start xb
print resultant substring from end efdgstb
Example 2: In this example, we will see how to create a string by taking characters from a certain positional gap.
Python3
ini_string = 'xbzefdgstb'
print ( "initial_strings : " , ini_string)
sstring_alt = ini_string[:: 2 ]
sstring_gap2 = ini_string[:: 3 ]
print ( "print resultant substring from start" , sstring_alt)
print ( "print resultant substring from end" , sstring_gap2)
|
Output:
initial_strings : xbzefdgstb
print resultant substring from start xzfgt
print resultant substring from end xegb
Example 3: In this example, we are considering both cases of taking strings from the middle with some positional gap between characters.
Python3
ini_string = 'xbzefdgstb'
print ( "initial_strings : " , ini_string)
sstring = ini_string[ 2 : 7 : 2 ]
print ( "print resultant substring" , sstring)
|
Output:
initial_strings : xbzefdgstb
print resultant substring zfg
Approach #4: Using list slicing to get substrings from a given string:
Approach:
Step size of 1 (no skipping):
This approach selects every character within the specified range of the string.
Step size of 2 (select every other item):
This approach selects every other character within the specified range of the string.
Step size of 3 (select every third item):
This approach selects every third character within the specified range of the string.
Step size of -1 (reverse order):
This approach selects every character within the specified range of the string in reverse order.
Step size of -2 (reverse order, select every other item):
This approach selects every other character within the specified range of the string in reverse order.
Step size of -3 (reverse order, select every third item):
This approach selects every third character within the specified range of the string in reverse order.
Python3
myString = "Hello World"
substring = myString[ 0 : 3 ]
print (substring)
myString = "abcdefghijklmnopqrstuvwxyz"
substring = myString[ 0 : len (myString): 2 ]
print (substring)
myString = "abcdefghijklmnopqrstuvwxyz"
substring = myString[ 3 : len (myString): 3 ]
print (substring)
|
OutputHel
acegikmoqsuwy
dgjmpsvy
When the step size is 1 (i.e., no skipping), list slicing has a time complexity of O(stop-start) and a space complexity of O(stop-start), since it creates a new list containing the sliced elements.
When the step size is greater than 1, the time complexity of list slicing depends on the size of the sliced substring, since it only creates a new list containing the selected elements
. The space complexity is O(stop-start/step), since it only creates a new list containing a fraction of the original elements.
Using a negative step size allows you to slice the string in reverse order. The time and space complexity are the same as for a positive step size.