Slicing through python

string[start : end : step]
#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)
str ="Aditi"
print("Original String :")
print(str)
#reversing the string using negative index slicing
print("Reverse String :")
print(str[: : -1])
output: itidA
  1. Positive Index Number
  2. Negative Index Number
# declaring the string
str = "Aditi"
#accessing the character of str at 0th index
print(str[0])
output:A
# declaring the string
str = "Aditi"
#accessing the character of str at last index
print(str[-1])
output:i
  • 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"
  1. import re module
  2. create a regex object with re.compile function()
  3. pass the string you want to search in regex objects search() function it will return a match object.
  4. call the match objects group to return a string of actual matched text.
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

--

--

--

A curious learner , an engineer by profession , exploring the world through writings

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

DeScientists, unite!

PDF Table Extractor

Introducing Konnect

Why every IT team needs a FinOps specialist

🎉 RESHIBA MEME competition is successfully completed! 💰 3 WINNERS — 3 X $50 worth of RESHIBA 💰

Elasticsearch - Defining the mapping of Twitter Data

Django Pagination

How can application re-engineering help transform your business? — Zerone Consulting

application re-engineering

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditi Pateriya

Aditi Pateriya

A curious learner , an engineer by profession , exploring the world through writings

More from Medium

A Ready Reckoner To Upgrade From Python 2 to Python 3

Python sigma rule #1

Python multiprocessing and why it’s not always the best solution

Mastering Pandas — Part 2: Pandas Series