->Python supports Positive direction which starts from 0 to end-1 and Python
also supports Negative direction which starts from -1 to end+1
Ex1:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('=====Using + direction====')
print(str1[0])
print(str1[1])
print(str1[2])
print(str1[3])
print()
print("======Using - direction======")
print(str1[-11])
print(str1[-10])
print(str1[-9])
print(str1[-8])
print()
time.sleep(2)
print('End of an application')
OUTPUT:
=====
C:\Users\Admin\Desktop\Python_Development>Python ihub1.py
Core Python
<class 'str'>
=====Using + direction====
C
o
r
e
======Using - direction======
C
o
r
e
End of an application
->Slice operator:
===========
Python supports Slice operator.The main objective of slice operator is to make the
pieces of the string as per the application reqn.Slice operator is applicable for +
direction and
- direction as well.Following is the syntax for slice operator
str1[begin:end(end-1):step]
Slice operator with + direction:
===================
Form_1:
=====
str1[begin:]
Here end(end-1) and step value is optional and the output of the string would from
given position to till end of the string
str1[2:]
Ex1:
==
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_1=====")
print(str1[2:])
print()
time.sleep(2)
print('End of an application')
Ex2:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_1=====")
print(str1[0:])
print()
time.sleep(2)
print('End of an application')
Ex3:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_1=====")
print(str1[5:])
print()
time.sleep(2)
print('End of an application')
Ex4:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_1=====")
print(str1[10:])
print()
time.sleep(2)
print('End of an application')
Ex5:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_1=====")
print(str1[11:])
print()
time.sleep(2)
print('End of an application')
Form_2:
=====
str1[:end(end-1)]
Here begin and step value is optional.The output of the string would be from
indexing position(0) to till end-1 th position.
str1[:6] ---->6-1=5 ---->0 to 5
Ex1:
==
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_2=====")
print(str1[:6])
print()
time.sleep(2)
print('End of an application')
Ex2:
==
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_2=====")
print(str1[:11])
print()
time.sleep(2)
print('End of an application')
Ex3:
==
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_2=====")
print(str1[:1])
print()
time.sleep(2)
print('End of an application')
Ex4:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_2=====")
print(str1[:15])
print()
time.sleep(2)
print('End of an application')
Ex5:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_2=====")
print(str1[:3])
print()
time.sleep(2)
print('End of an application')
Form_3:
=====
str1[begin:end(end-1)
Here step is optional.The output of the string would from given position to till
end-1 th position
str1[3:9] --->9-1=8
Ex1:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_3=====")
print(str1[3:9])
print()
time.sleep(2)
print('End of an application')
Ex2:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_3=====")
print(str1[0:11])
print()
time.sleep(2)
print('End of an application')
Ex3:
==
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_3=====")
print(str1[5:11])
print()
time.sleep(2)
print('End of an application')
Ex4:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_3=====")
print(str1[9:11])
print()
time.sleep(2)
print('End of an application')
Form_4:
=====
str1[begin:end(end-1):step]
Here output will be display on top of step value.
str1[0:11:1]
Ex1:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_4=====")
print(str1[0:11:1])
print()
time.sleep(2)
print('End of an application')
Ex2:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_4=====")
print(str1[0:11:2])
print()
time.sleep(2)
print('End of an application')
Ex3:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_4=====")
print(str1[0:11:3])
print()
time.sleep(2)
print('End of an application')
Ex4:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_4=====")
print(str1[5:11:3])
print()
time.sleep(2)
print('End of an application')
Ex5:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_4=====")
print(str1[0:11:5])
print()
time.sleep(2)
print('End of an application')
Form_5:
======
str1[0:]
str1[:]
str1[::] ---->Core Python
Ex1:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form_5=====")
print(str1[0:])
print()
print(str1[:])
print()
print(str1[::])
print()
time.sleep(2)
print('End of an application')
Ex2:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE_OPERATOR USING + DIRECTION====")
print()
print("====Form=====")
print(str1[1:9:0])
print()
time.sleep(2)
print('End of an application')
OUTPUT:
=====
C:\Users\Admin\Desktop\Python_Development>Python ihub1.py
Core Python
<class 'str'>
====SLICE_OPERATOR USING + DIRECTION====
====Form=====
Traceback (most recent call last):
File "ihub1.py", line 10, in <module>
print(str1[1:9:0])
ValueError: slice step cannot be zero
C:\Users\Admin\Desktop\Python_Development>
Ex3:
===
import time
str1="Core Python"
print("Reverse of a string is:",str1[::-1])
print()
time.sleep(2)
print('End of an application')
Ex4:
===
import time
str1="Core Python"
print("Our string object is in asending_order is:",str1[0:])
print()
print("Our String_Object is in decending_order is:",str1[::-1])
print()
time.sleep(2)
print('End of an application')
Slice operator with Negative direction:
========================
Python supports Slice operartor with Negative direction which starts from -1 to
end+1.While working with slice operator with negative direction our step value must
be negative number otherwise most of the time output will be empty.
str1[-1:-7:-1] --->-7+1=-6
Ex1:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[-1:-7:-1])
print()
time.sleep(2)
print('End of an application')
Ex2:
==
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[-1:-7:-2])
print()
time.sleep(2)
print('End of an application')
Ex3:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[-1:-9:-3])
print()
time.sleep(2)
print('End of an application')
Ex4:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[-1:-7:0])
print()
time.sleep(2)
print('End of an application')
Ex5:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[-1:-7:1])
print()
time.sleep(2)
print('End of an application')
Ex6:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[-1:-7])
print()
time.sleep(2)
print('End of an application')
Ex7:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[-1:])
print()
time.sleep(2)
print('End of an application')
Ex8:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[-2:])
print()
time.sleep(2)
print('End of an application')
Ex9:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[-6:])
print()
time.sleep(2)
print('End of an application')
Ex10:
====
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[:-3])
print()
time.sleep(2)
print('End of an application')
Ex11:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[:-5])
print()
time.sleep(2)
print('End of an application')
Ex12:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[:-9])
print()
time.sleep(2)
print('End of an application')
Ex13:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[::-1])
print()
time.sleep(2)
print('End of an application')
Ex14:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[::-2])
print()
time.sleep(2)
print('End of an application')
Ex15:
===
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[::-3])
print()
time.sleep(2)
print('End of an application')
Ex16:
====
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[::1])
print()
time.sleep(2)
print('End of an application')
Ex17:
====
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("====SLICE OPERATOR WITH - DIRECTION====")
print()
print(str1[::2])
print()
time.sleep(2)
print('End of an application')
input():
=====
It is a predefine function in Python.The main objective of input() function is to
read the
dynamically value from the keyboard.While working with input() function if we are
reading
any data or information from keyboard PVM will consider by default string type.
Ex1:
==
import time
X1=input("Enter the value:")
print(X1)
print()
print(type(X1))
print()
time.sleep(2)
print('End of an application')
OUTPUT:
=====
C:\Users\Admin\Desktop\Python_Development>Python ihub1.py
Enter the value:100
100
<class 'str'>
End of an application
C:\Users\Admin\Desktop\Python_Development>Python ihub1.py
Enter the value:123.45
123.45
<class 'str'>
End of an application
C:\Users\Admin\Desktop\Python_Development>Python ihub1.py
Enter the value:True
True
<class 'str'>
End of an application
C:\Users\Admin\Desktop\Python_Development>
Ex2:
===
import time
X1=input("Enter the value of X1:")
X2=input("Enter the value of X2:")
res1=X1+X2
print("The result_set is:",res1)
print()
time.sleep(2)
print('End of an application')
Q1)Why Typecasting is required in Python
-----------------------------------
Ex1:
===
import time
print("=====Before_TypeCasting=====")
X1=input('Enter the value of X1:')
X2=input('Enter the value of X2:')
print(X1)
print()
print(type(X1))
print()
print(X2)
print()
print(type(X2))
print()
res1=X1+X2
print("The result_set is:",res1)
print()
print(type(res1))
print()
print("====After_Typecasting====")
X1=int(input('Enter the value of X1:'))
X2=int(input('Enter the value of X2:'))
print(X1)
print()
print(type(X1))
print()
print(X2)
print()
print(type(X2))
print()
res1=X1+X2
print("The result_set is:",res1)
print()
print(type(res1))
print()
time.sleep(2)
print('End of an application')
OUTPUT:
======
C:\Users\Admin\Desktop\Python_Development>Python ihub1.py
=====Before_TypeCasting=====
Enter the value of X1:10
Enter the value of X2:20
10
<class 'str'>
20
<class 'str'>
The result_set is: 1020
<class 'str'>
====After_Typecasting====
Enter the value of X1:10
Enter the value of X2:20
10
<class 'int'>
20
<class 'int'>
The result_set is: 30
<class 'int'>
End of an application
C:\Users\Admin\Desktop\Python_Development>
Typecasting:
========
Python supports Typecasting.It is the process or methodlogy to convert the
one type of data into another type of data using following functions
->int()
->float()
->complex()
->bool()
->str()