Slicing through python
what are slicing and indexing in python ?
Slicing
By using the Slicing feature in Python you can access parts of a sequence. We use slicing when we require a part of string and not the complete string.Python supports slice notation for any sequential data type like lists, strings, tuples, bytes, bytearrays, and ranges.its a tool that builds new lists out of an existing list.
syntax for slicing :
string[start : end : step]
start: you can specify the starting index here from where you want to initiate the slicing.
end :you can specify the ending index +1 here , as slicing cuts the data structure from start to end-1.
step:determines the increment between each index for slicing.
Example
#declaring a string
str='Aditi pateriya'
#slicing the string
print(str[:4])
print (str[1:5])
Output:
Adit( from index 0-3)
diti( from index 1-4)
slicing with negative index
You can use negative indexes to slice a string or list. -1 represents the last letter of the string , -2 second last and so on. Heres how to use negative indexing to reverse a string.
str ="Aditi"
print("Original String :")
print(str)
#reversing the string using negative index slicing
print("Reverse String :")
print(str[: : -1])
output: itidA
Indexing
Indexing means referring to an element of a sequence by its position within the sequence. Each of a string’s characters corresponds to an index number and each character can be accessed using their index number.
you can access characters in a String in Two ways :
- Positive Index Number
- Negative Index Number
# declaring the string
str = "Aditi"
#accessing the character of str at 0th index
print(str[0])
output:A
Positive indexing starts with the first letter being at index 0 and so on.
# declaring the string
str = "Aditi"
#accessing the character of str at last index
print(str[-1])
output:i
Negative indexing beings with the last letter at index -1.
Exception Handling in python
Even if the syntax of a statement or expression is correct, it may still cause an error when executed. Python exceptions are errors that are detected during execution time.
to handle these exceptions here are 4 main components
Some of the most recurring errors which cause an exception include:
- Zero Division Error
- OverFlow Error
- Floating Point Error
# Python program to handle simple runtime error
a = [1, 2]
try:
print "Second element = %d" %(a[1])
# Throws error since there are only 2 elements in array
print "Third element = %d" %(a[2])
except IndexError:
print "error has occurred"
Regular expression in python
A regular expression is a special sequence of characters that helps you match or find other strings using a specialized pattern.The Python module re is imported for the usage of regular expressions.
To use regular expressions for pattern matching
- import re module
- create a regex object with re.compile function()
- pass the string you want to search in regex objects search() function it will return a match object.
- call the match objects group to return a string of actual matched text.
here's an example:
import re
Phonenoregex=re.compile('r,\d\d\d-\d\d\d-\d\d\d\d')
mo=Phonenoregex.search('My no is 414-222-1234.')
print('Phone no found '+mo.group)
output:
Phone no found 414-222-1234
This code snippet matches the phone number pattern in the string and sipalys the matched pattern.