1691912901477_Python_Basics and list,tuple,string.pptx

KUSHSHARMA630049 34 views 79 slides May 09, 2024
Slide 1
Slide 1 of 79
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79

About This Presentation

python basics


Slide Content

Python Basics

Content Comments in Python Variable Declaration in Python Data Types in Python Type Conversion in Python Operators in Python

Comments in Python In general, Comments are used in a programming language to describe the program or to hide the some part of code from the interpreter. Comments in Python can be used to explain any program code. It can also be used to hide the code as well. Comment is not a part of the program, but it enhances the interactivity of the program and makes the program readable. Python supports two types of comments: Single Line Comment Multi Line Comment

Comments in Python Cont.. Single Line Comment: In case user wants to specify a single line comment, then comment must start with ‘#’ Example: # This is single line comment  print "Hello Python"   Output: Hello Python Multi Line Comment: Multi lined comment can be given inside triple quotes. Example: '''This is  Multiline  Comment'''   print "Hello Python"   Output: Hello Python

Variable Declaration in Python A variable is a named memory location in which we can store values for the particular program. In other words, Variable is a name which is used to refer memory location. Variable also known as identifier and used to hold value. In Python, We don't need to declare explicitly variable in Python. When we assign any value to the variable that variable is declared automatically. In Python, We don't need to specify the type of variable because Python is a loosely typed language.

Variable Declaration in Python Cont.. In loosely typed language no need to specify the type of variable because the variable automatically changes it's data type based on assigned value. Rules for naming variable: Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore. It is recommended to use lowercase letters for variable name. ‘SUM’ and ‘sum’ both are two different variables. Example: Vardemo.py a=10 #integer b=“python" #string c=12.5 #float print(a) print(b) print(c) output: $python3 Vardemo.py 10 python 12.5

Variable Declaration in Python Cont.. Python allows us to assign a value to multiple variables and multiple values to multiple variables in a single statement which is also known as multiple assignment. Assign single value to multiple variables : Assign multiple values to multiple variables : Example: Vardemo1.py x=y=z=50 print x print y print z output: $python3 Vardemo1.py 50 50 50 Example: Vardemo2.py a,b,c =5,10,15 print a print b print c output: $python3 Vardemo2.py 5 10 15

Data Types in Python In general, Data Types specifies what type of data will be stored in variables. Variables can hold values of different data types. Python is a dynamically typed or loosely typed language, hence we need not define the type of the variable while declaring it. The interpreter implicitly binds the value with its type. Python provides us the  type ()  function which enables us to check the type of the variable.

Data Types in Python Cont.. Python provides following standard data types , those are Numbers String Numbers: Number stores numeric values. Python creates Number type variable when a number is assigned to a variable. There are three numeric types in Python: int float Complex

Data Types in Python Cont..     2. float: Float or "floating point number" is a number, positive or negative, containing decimals. Example: X=1.0 Y=12.3 Z=-13.4 3. complex: Complex numbers are written with a "j" as the imaginary part. Example: A=2+5j B=-3+4j C=-6j 1. int: Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. Example:  a=10 b=-12 c=123456789

Data Types in Python Cont.. String: The string can be defined as the sequence of characters represented in the quotation marks. In python, we can use single, double, or triple quotes to define a string. In the case of string handling, the operator + is used to concatenate two strings as the operation  "hello"+" python"  returns  "hello python" .     Example: S1=‘Welcome’ #using single quotes S2=“To” #using double quotes S3=‘’’Python’’’ #using triple quotes

Data Types in Python Cont.. Example: “ datatypesdemo.py ” a=10 b=“Python" c = 10.5 d=2.14j print("Data type of Variable a :",type(a)) print("Data type of Variable b :",type(b)) print("Data type of Variable c :",type(c)) print("Data type of Variable d :",type(d)) Output: python3 datatypesdemo.py   Datatype of Variable a : <class ‘ int ’> Datatype of Variable b : <class ‘ str ’> Datatype of Variable c : <class ‘float’> Datatype of Variable d : <class ‘complex’>

Type Conversion in Python Python provides Explicit type conversion functions to directly convert one data type to another. It is also called as  Type Casting  in Python Python supports following functions int ()  : This function converts  any data type to integer. float()  : This function is used to convert  any data type to a floating point number. str() : This function is used to convert  any data type to a string. Example: “ Typeconversiondemo.py ” x = int(2.8) y = int("3") z = float(2) s = str (10) print(x);print(y) print(z); print(s) Output: python3 typeconversiondemo.py 2 3 2 10  

List in Python

List Creation

List in Python In python, a list can be defined as a collection of values or items. The items in the list are separated with the comma (,) and enclosed with the square brackets [ ]. Syntax: listname = [ value1 , value2 , value3 , …. ] Example: “ listdemo.py ” L1 = [] L2 = [ 123 , "python" , 3 . 7 ] L3 = [ 1 , 2 , 3 , 4 , 5 , 6 ] L4 = [ "C" , "Java" , "Python" ] print (L1) print (L2) print (L3) print (L4) Output: python listdemo.py [] [ 123 , 'python' , 3 . 7 ] [ 1 , 2 , 3 , 4 , 5 , 6 ] [ ' C' , 'Java' , 'Python ' ]

List Indexing

List Indexing in Python Like string sequence, the indexing of the python lists starts from 0, i.e. the first element of the list is stored at the 0th index, the second element of the list is stored at the 1st index, and so on. The elements of the list can be accessed by using the slice operator []. Example: mylist =[‘ banana’,’apple’,’mango’,’tomato’,’berry ’] mylist [0]=”banana” mylist [1:3]=[” apple”,”mango ”] mylist [2]=”mango”

List Indexing in Python cont… Unlike other languages, python provides us the flexibility to use the negative indexing also. The negative indices are counted from the right. The last element (right most) of the list has the index -1, its adjacent left element is present at the index -2 and so on until the left most element is encountered. Example: mylist =[‘ banana’,’apple’,’mango’,’tomato’,’berry ’] mylist [-1]=”berry” mylist [-4:-2]=[“ apple”,mango ”] mylist [-3]=”mango”

List Operators

List Operators in Python + It is known as concatenation operator used to concatenate two lists. * It is known as repetition operator. It concatenates the multiple copies of the same list. [] It is known as slice operator. It is used to access the list item from list. [:] It is known as range slice operator. It is used to access the range of list items from list. in It is known as membership operator. It returns if a particular item is present in the specified list. not in It is also a membership operator and It returns true if a particular list item is not present in the list.

List Operators in Python cont… Example: “listopdemo.py” num=[ 1 , 2 , 3 , 4 , 5 ] lang =[ ' python' , 'c' , 'java' , 'php ' ] print (num + lang ) #concatenates two lists print (num * 2 ) #concatenates same list 2 times print ( lang [ 2 ]) # prints 2nd index value print ( lang [ 1 : 4 ]) #prints values from 1st to 3rd index. print ( ' cpp ' in lang ) # prints False print ( 6 not in num) # prints True Output: python listopdemo.py [ 1 , 2 , 3 , 4 , 5 , 'python' , 'c' , 'java' , ' php ' ] [ 1 , 2 , 3 , 4 , 5 , 1 , 2 , 3 , 4 , 5 ] java [ 'c' , 'java' , ' php ' ] False True

How to update or change elements to a list? Python allows us to modify the list items by using the slice and assignment operator. We can use assignment operator ( = ) to change an item or a range of items. Example: “listopdemo1.py” num=[ 1 , 2 , 3 , 4 , 5 ] print (num) num[ 2 ]= 30 print (num) num[ 1 : 3 ]=[ 25 , 36 ] print (num) num[ 4 ]= "Python" print (num) Output: python listopdemo1.py [ 1 , 2 , 3 , 4 , 5 ] [ 1 , 2 , 30 , 4 , 5 ] [ 1 , 25 , 36 , 4 , 5 ] [ 1 , 25 , 36 , 4 , 'Python' ]

How to delete or remove elements from a list? Python allows us to delete one or more items in a list by using the del keyword. Example: “listopdemo2.py” num = [ 1 , 2 , 3 , 4 , 5 ] print (num) del num[ 1 ] print (num) del num[ 1 : 3 ] print (num) Output: python listopdemo2.py [ 1 , 2 , 3 , 4 , 5 ] [ 1 , 3 , 4 , 5 ] [ 1 , 5 ]

List Functions & Methods

List Functions & Methods in Python Python provides various in-built functions and methods which can be used with list. Those are ☞ len():   In Python, len () function is used to find the length of list,i.e it returns the number of items in the list.. Syntax: len ( list ) len () max() min() sum() list() Example: listlendemo.py num=[ 1 , 2 , 3 , 4 , 5 , 6 ] print ( "length of list :" , len (num)) Output: python listlendemo.py length of list : 6 sorted() append() remove() sort() reverse() count() index() insert() pop() clear()

List Functions & Methods in Python Cont.. ☞ max (): In Python, max() function is used to find maximum value in the list. Syntax: max( list ) Example: listmaxdemo.py list1=[ 1 , 2 , 3 , 4 , 5 , 6 ] list2=[ 'java' , 'c' , 'python' , 'cpp' ] print ( "Max of list1 :" , max (list1)) print ( "Max of list2 :" , max (list2)) Output: python listmaxdemo.py Max of list1 : 6 Max of list2 : python

List Functions & Methods in Python Cont.. ☞ min (): In Python, min() function is used to find minimum value in the list. Syntax: min( list ) Example: listmindemo.py list1=[ 1 , 2 , 3 , 4 , 5 , 6 ] list2=[ ' java' , 'c' , 'python' , 'cpp ' ] print ( "Min of list1 :" , min (list1)) print ( "Min of list2 :" , min (list2)) Output: python listmindemo.py Min of list1 : 1 Min of list2 : c

List Functions & Methods in Python Cont.. ☞ sum (): In python, sum() function returns sum of all values in the list. List values must in number type. Syntax: sum( list ) Example: listsumdemo.py list1=[ 1 , 2 , 3 , 4 , 5 , 6 ] print ( "Sum of list items :" , sum (list1)) Output: python listsumdemo.py Sum of list items : 21

List Functions & Methods in Python Cont.. ☞ list (): In python, list() is used to convert given sequence (string or tuple) into list. Syntax: list( sequence ) Example: listdemo.py str = "python" list1= list ( str ) print (list1) Output: python listdemo.py [ 'p' , 'y' , 't' , 'h' , 'o' , 'n' ]

List Functions & Methods in Python Cont.. ☞ sorted (): In python, sorted() function is used to sort all items of list in an ascending order. Syntax: sorted( list ) Example: sorteddemo.py num=[ 1 , 3 , 2 , 4 , 6 , 5 ] lang =[ 'java' , 'c' , 'python' , 'cpp' ] print ( sorted (num)) print ( sorted ( lang )) Output: python sorteddemo.py [ 1 , 2 , 3 , 4 , 5 , 6 ] [ 'c' , ' cpp ' , 'java' , 'python' ]

List Functions & Methods in Python Cont.. ☞ append (): In python, append() method adds an item to the end of the list. Syntax: list .append( item ) where item may be number, string, list and etc. Example: appenddemo.py num=[ 1 , 2 , 3 , 4 , 5 ] lang =[ ' python' , 'java ' ] num. append ( 6 ) print (num) lang. append ( " cpp " ) print ( lang ) Output: python appenddemo.py [ 1 , 2 , 3 , 4 , 5 , 6 ] [ 'python' , 'java' , ' cpp ' ]

List Functions & Methods in Python Cont.. ☞ remove (): In python, remove() method removes item from the list. It removes first occurrence of item if list contains duplicate items. It throws an error if the item is not present in the list. Syntax: list .remove ( item ) Example: removedemo.py list1=[ 1 , 2 , 3 , 4 , 5 ] list2=[ 'A' , 'B' , 'C' , 'B' , 'D' ] list1. remove ( 2 ) print (list1) list2. remove ( "B" ) print (list2) list2. remove ( "E" ) print (list2) Output: python removedemo.py [ 1 , 3 , 4 , 5 ] [ 'A' , 'C' , 'B' , 'D' ] ValueError : x not in list

List Functions & Methods in Python Cont.. ☞ sort (): In python, sort() method sorts the list elements into descending or ascending order. By default, list sorts the elements into ascending order. It takes an optional parameter 'reverse' which sorts the list into descending order. Syntax: list .sort ( [reverse=true]) Example: sortdemo.py list1=[ 6 , 8 , 2 , 4 , 10 ] list1. sort () print ( "\n After Sorting:\n" ) print (list1) print ( "Descending Order:\n" ) list1. sort ( reverse = True ) print (list1) Output: python sortdemo.py After Sorting: [ 2 , 4 , 6 , 8 , 10 ] Descending Order: [ 10 , 8 , 6 , 4 , 2 ]

List Functions & Methods in Python Cont.. ☞ reverse (): In python, reverse() method reverses elements of the list. i.e. the last index value of the list will be present at 0th index. Syntax: list .reverse () Example: reversedemo.py list1=[ 6 , 8 , 2 , 4 , 10 ] list1. reverse () print ( "\n After reverse:\n" ) print (list1) Output: python reversedemo.py After reverse: [ 10 , 4 , 2 , 8 , 6 ]

List Functions & Methods in Python Cont.. ☞ count(): In python, count() method returns the number of times an element appears in the list. If the element is not present in the list, it returns 0. Syntax: list .count( item ) Example: countdemo.py num=[ 1 , 2 , 3 , 4 , 3 , 2 , 2 , 1 , 4 , 5 , 8 ] cnt = num. count ( 2 ) print ( "Count of 2 is:" , cnt ) cnt = num. count ( 10 ) print ( "Count of 10 is:" , cnt ) Output: python countdemo.py Count of 2 is: 3 Count of 10 is:

List Functions & Methods in Python Cont.. ☞ index(): In python, index () method returns index of the passed element. If the element is not present, it raises a ValueError . If list contains duplicate elements, it returns index of first occurred element. This method takes two more optional parameters start and end which are used to search index within a limit. Syntax: list . index( item [, start[, end]] ) Example: indexdemo.py list1=[ ' p' , 'y' , 't' , 'o' , 'n' , 'p ' ] print (list1. index ( 't' )) Print (list1. index ( 'p' )) Print (list1. index ( 'p' , 3 , 10 )) Print (list1. index ( 'z' )) ) Output: python indexdemo.py 2 5 Value Error

List Functions & Methods in Python Cont.. ☞ insert(): In python, insert() method inserts the element at the specified index in the list. The first argument is the index of the element before which to insert the element. Syntax: list .insert( index,item ) Example: insertdemo.py num=[ 10 , 20 , 30 , 40 , 50 ] num. insert ( 4 , 60 ) print (num) num. insert ( 7 , 70 ) print (num) Output: python insertdemo.py [ 10 , 20 , 30 , 40 , 60 , 50 ] [ 10 , 20 , 30 , 40 , 60 , 50 , 70 ]

List Functions & Methods in Python Cont.. ☞ pop(): In python, pop() element removes an element present at specified index from the list. Syntax: list .pop( index ) Example: popdemo.py num=[ 10 , 20 , 30 , 40 , 50 ] num. pop () print (num) num. pop ( 2 ) print (num) num. pop ( 7 ) print (num) Output: python popdemo.py [ 10 , 20 , 30 , 40 ] [ 10 , 20 , 40 ] IndexError : Out of range

List Functions & Methods in Python Cont.. ☞ clear(): In python, clear() method removes all the elements from the list. It clears the list completely and returns nothing. Syntax: list .clear () Example: cleardemo.py num=[ 10 , 20 , 30 , 40 , 50 ] num. clear () print (num) Output: python cleardemo.py [ ]

Tuple Creation

Tuple in Python In python, a tuple is a sequence of immutable elements or items. Tuple is similar to list since the items stored in the list can be changed whereas the tuple is immutable and the items stored in the tuple cannot be changed. A tuple can be written as the collection of comma-separated values enclosed with the small brackets or parantheses ( ) . Syntax: var = ( value1 , value2 , value3 , …. ) Example: “ tupledemo.py ” t1 = () t2 = ( 123 , "python" , 3 . 7 ) t3 = ( 1 , 2 , 3 , 4 , 5 , 6 ) t4 = ( "C" ,) print (t1) print (t2) print (t3) print (t4) Output: python tupledemo.py () ( 123 , 'python' , 3 . 7 ) ( 1 , 2 , 3 , 4 , 5 , 6 ) ( 'C' ,)

Tuple Indexing

Tuple Indexing in Python Like list sequence, the indexing of the python tuple starts from 0, i.e. the first element of the tuple is stored at the 0th index, the second element of the tuple is stored at the 1st index, and so on. The elements of the tuple can be accessed by using the slice operator []. Example: mytuple =(‘ banana’,’apple’,’mango’,’tomato’,’berry ’) mytuple [0]=”banana” mytuple [1:3]=[” apple”,”mango ”] mytuple [2]=”mango”

Tuple Indexing in Python cont… Unlike other languages, python provides us the flexibility to use the negative indexing also. The negative indices are counted from the right. The last element (right most) of the tuple has the index -1, its adjacent left element is present at the index -2 and so on until the left most element is encountered. Example: mytuple =[‘ banana’,’apple’,’mango’,’tomato’,’berry ’] mytuple [-1]=”berry” mytuple [-4:-2]=[“ apple”,mango ”] mytuple [-3]=”mango”

Tuple Operators

Tuple Operators in Python + It is known as concatenation operator used to concatenate two tuples. * It is known as repetition operator. It concatenates the multiple copies of the same tuple. [] It is known as slice operator. It is used to access the item from tuple. [:] It is known as range slice operator. It is used to access the range of items from tuple. in It is known as membership operator. It returns if a particular item is present in the specified tuple. not in It is also a membership operator and It returns true if a particular item is not present in the tuple.

Tuple Operators in Python cont… Example: “tupleopdemo.py” num=( 1 , 2 , 3 , 4 , 5 ) lang =( ' python' , 'c' , 'java' , 'php ' ) print (num + lang ) #concatenates two tuples print (num * 2 ) #concatenates same tuple 2 times print ( lang [ 2 ]) # prints 2nd index value print ( lang [ 1 : 4 ]) #prints values from 1st to 3rd index. print ( ' cpp ' in lang ) # prints False print ( 6 not in num) # prints True Output: python tupleopdemo.py ( 1 , 2 , 3 , 4 , 5 , 'python' , 'c' , 'java' , ' php ' ) ( 1 , 2 , 3 , 4 , 5 , 1 , 2 , 3 , 4 , 5 ) java ( 'c' , 'java' , ' php ' ) False True

How to add or remove elements from a tuple? Unlike lists, the tuple items cannot be updated or deleted as tuples are immutable. To delete an entire tuple, we can use the del keyword with the tuple name. Example: “tupledemo1.py” tup =( ' python' , 'c' , 'java' , 'php ' ) tup [ 3 ]= "html" print ( tup ) del tup [ 3 ] print ( tup ) del tup Output: python tupledemo1.py 'tuple' object does not support item assignment 'tuple' object doesn't support item deletion

Tuple Functions & Methods

Tuples Functions & Methods in Python Python provides various in-built functions and methods which can be used with tuples. Those are ☞ len():   In Python, len () function is used to find the length of tuple,i.e it returns the number of items in the tuple. Syntax: len ( tuple ) len () max() min() sum() Example: lendemo.py num=( 1 , 2 , 3 , 4 , 5 , 6 ) print ( "length of tuple :" , len (num)) Output: python lendemo.py length of tuple : 6 tuple() sorted() count() index()

Tuple Functions & Methods in Python Cont.. ☞ max (): In Python, max() function is used to find maximum value in the tuple. Syntax: max( tuple ) Example: maxdemo.py t1=( 1 , 2 , 3 , 4 , 5 , 6 ) t2=( ' java' , 'c' , 'python' , 'cpp ' ) print ( "Max of Tuple t1 :" , max (t1)) print ( "Max of Tuple t2 :" , max (t2)) Output: python maxdemo.py Max of Tuple t1 : 6 Max of Tuple t2 : python

Tuple Functions & Methods in Python Cont.. ☞ min (): In Python, min() is used to find minimum value in the tuple. Syntax: min( tuple ) Example: mindemo.py t1=( 1 , 2 , 3 , 4 , 5 , 6 ) t2=( ' java' , 'c' , 'python' , 'cpp ' ) print ( "Min of Tuple t1 :" , min (t1)) print ( "Min of Tuple t2 :" , min (t2)) Output: python mindemo.py Min of Tuple t1 : 1 Min of Tuple t2 : c

Tuple Functions & Methods in Python Cont.. ☞ sum (): In python, sum() function returns sum of all values in the tuple. The tuple values must in number type. Syntax: sum( tuple ) Example: sumdemo.py t1=( 1 , 2 , 3 , 4 , 5 , 6 ) print ( "Sum of tuple items :" , sum (t1)) Output: python sumdemo.py Sum of tuple items : 21

Tuple Functions & Methods in Python Cont.. ☞ tuple (): In python, tuple() is used to convert given sequence (string or list) into tuple. Syntax: tuple( sequence ) Example: tupledemo.py str = "python" t1= tuple ( str ) print (t1) num=[ 1 , 2 , 3 , 4 , 5 , 6 ] t2= tuple (num) print (t2) Output: python tupledemo.py ( 'p' , 'y' , 't' , 'h' , 'o' , 'n‘ ) ( 1 , 2 , 3 , 4 , 5 , 6 )

Tuple Functions & Methods in Python Cont.. ☞ sorted (): In python, sorted() function is used to sort all items of tuple in an ascending order. Syntax: sorted( tuple ) Example: sorteddemo.py num=( 1 , 3 , 2 , 4 , 6 , 5 ) lang =( ' java' , 'c' , 'python' , 'cpp ' ) print ( sorted (num)) print ( sorted ( lang )) Output: python sorteddemo.py ( 1 , 2 , 3 , 4 , 5 , 6 ) ( 'c' , ' cpp ' , 'java' , 'python' )

Tuple Functions & Methods in Python Cont.. ☞ count(): In python, count() method returns the number of times an element appears in the tuple. If the element is not present in the tuple, it returns 0. Syntax: tuple .count ( item ) Example: countdemo.py num=( 1 , 2 , 3 , 4 , 3 , 2 , 2 , 1 , 4 , 5 , 8 ) cnt = num. count ( 2 ) print ( "Count of 2 is:" , cnt ) cnt = num. count ( 10 ) print ( "Count of 10 is:" , cnt ) Output: python countdemo.py Count of 2 is: 3 Count of 10 is:

Tuple Functions & Methods in Python Cont.. ☞ index(): In python, index () method returns index of the passed element. If the element is not present, it raises a ValueError . If tuple contains duplicate elements, it returns index of first occurred element. This method takes two more optional parameters start and end which are used to search index within a limit. Syntax: tuple .index ( item [, start[, end]] ) Example: indexdemo.py t1=( ' p' , 'y' , 't' , 'o' , 'n' , 'p ' ) print (t1. index ( 't' )) Print (t1. index ( 'p' )) Print (t1. index ( 'p' , 3 , 10 )) Print (t1. index ( 'z' )) ) Output: python indexdemo.py 2 5 Value Error

String Declaration

Strings in Python In python, strings can be created by enclosing the character or the sequence of characters in the quotes. Python allows us to use single quotes, double quotes, or triple quotes to create strings. In python, strings are treated as the sequence of characters which means that python doesn't support the character data type instead a single character written as 'p' is treated as the string of length 1. Example: str1 = 'Hello Python' str2 = "Hello Python" str3 = '''Hello Python'''

String Indexing

String Indexing in Python Like other programming languages, the indexing of the python strings starts from 0. For example , the string "HELLO" is indexed as given in the below figure. str = "HELLO" str [ ]=H str [ 1 ]=E str [ 4 ]=O Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on. str [- 1 ]=O str [- 2 ]=L str [- 4 ]=E

String Operators

String Operators in Python + It is known as concatenation operator used to join the strings. * It is known as repetition operator. It concatenates the multiple copies of the same string. [ ] It is known as slice operator. It is used to access the sub-strings of a particular string. [ : ] It is known as range slice operator. It is used to access the characters from the specified range. in It is known as membership operator. It returns if a particular sub-string is present in the specified string. not in It is also a membership operator and does the exact reverse of in. It returns true if a particular substring is not present in the specified string. r/R It is used to specify the raw string. To define any string as a raw string, the character r or R is followed by the string. Such as "hello \n python". % It is used to perform string formatting. It makes use of the format specifies used in C programming like %d or %f to map their values in python.

String Operators in Python cont… Example: “stropdemo.py” str1 = "Hello" str2 = " World" print (str1* 3 ) # prints HelloHelloHello print (str1+str2) # prints Hello world print (str1[ 4 ]) # prints o print (str1[ 2 : 4 ]) # prints ll print ( 'w' in str1) # prints false as w is not present in str1 print ( ' Wo ' not in str2) # prints false as Wo is present in str2. print ( r 'Hello \n world' ) # prints Hello\n world as it is written print ( "The string str1 : %s" %(str1)) # prints The string str : Hello Output: python ifdemo.py HelloHelloHello Hello World o ll False False Hello\n world The string str1 : Hello

String Functions & Methods

String Functions & Methods in Python Python provides various in-built functions & methods that are used for string handling. Those are ☞ len():   In python, len() function returns length of the given string. Syntax: len ( string ) len() lower() upper() replace() join() split() find() index() isalnum() isdigit() isnumeric() islower() isupper() Example: strlendemo.py str1= "Python Language" print ( len (str1)) Output: python strlendemo.py 15

String Functions & Methods in Python Cont.. ☞ lower (): In python, lower() method returns all characters of given string in lowercase. Syntax: str . lower () ☞ upper (): In python, upper() method returns all characters of given string in uppercase. Syntax: str . upper () Example: strlowerdemo.py str1 = " PyTHOn " print ( str1 . lower ()) Output: python strlowerdemo.py python Example: strupperdemo.py str = " PyTHOn " print ( str . upper ()) Output: python strupperdemo.py PYTHON

String Functions & Methods in Python Cont.. ☞ replace() In python, replace() method replaces the old sequence of characters with the new sequence. Syntax: str .replace(old, new [, count ]) Example: strreplacedemo.py str = "Java is Object-Oriented Java" str2 = str .replace( " Java" , "Python " ) print ( "Old String: \ n" , str ) print ( "New String: \n" ,str2) str3 = str .replace( "Java" , "Python" , 1 ) print ( "\n Old String: \ n" , str ) print ( "New String: \n" ,str3) Output: python strreplacedemo.py Old String: Java is Object-Oriented and Java New String: Python is Object-Oriented and Python Old String: Java is Object-Oriented and Java New String: Python is Object-Oriented and Java

String Functions & Methods in Python Cont.. ☞ split(): In python, split() method splits the string into a comma separated list. The string splits according to the space if the delimiter is not provided. Syntax: str . split ([sep= "delimiter" ]) Example: strsplitdemo.py str1 = "Python is a programming language" str2 = str1. split () print (str1); print (str2) str1 = "Python,is,a,programming,language" str2 = str1. split (sep= ',' ) print (str1); print (str2) Output: python strsplitdemo.py Java is a programming language [ 'Java' , 'is' , 'a' , 'programming' , 'language' ] Java, is , a, programming, language [ 'Java' , 'is' , 'a' , 'programming' , 'language' ]

String Functions & Methods in Python Cont.. ☞ find(): In python, find() method finds substring in the given string and returns index of the first match. It returns -1 if substring does not match. Syntax: str . find ( sub [, start [, end ]]) Example: strfinddemo.py str1 = "python is a programming language" str2 = str1. find ( "is" ) str3 = str1. find ( "java" ) str4 = str1. find ( "p" , 5 ) str5 = str1. find ( "i" , 5 , 25 ) print (str2,str3,str4,str5) Output: python strfinddemo.py 7 -1 12 7

String Functions & Methods in Python Cont.. ☞ index(): In python, index() method is same as the find() method except it returns error on failure. This method returns index of first occurred substring and an error if there is no match found. Syntax: str . index ( sub [, start [, end ]]) Example: strindexdemo.py str1 = "python is a programming language" str2 = str1. index ( "is" ) print (str2) str3 = str1. index ( "p" , 5 ) print (str3) str4 = str1. index ( "i" , 5 , 25 ) print (str4) str5 = str1. index ( "java" ) print (str5) Output: python strindexdemo.py 7 12 7 Substring not found

String Functions & Methods in Python Cont.. ☞ isalnum(): In python, isalnum() method checks whether the all characters of the string is alphanumeric or not. A character which is either a letter or a number is known as alphanumeric. It does not allow special chars even spaces. Syntax: str .isalnum () Example: straldemo.py str1 = "python" str2 = "python123" str3 = "12345" str4 = "python@123" str5 = "python 123" print (str1. isalnum()) print (str2. isalnum()) print (str3. isalnum()) print (str4. isalnum()) print (str5. isalnum()) Output: python straldemo.py True True True False False

String Functions & Methods in Python Cont.. ☞ isdigit(): In python, isdigit() method returns True if all the characters in the string are digits. It returns False if no character is digit in the string. Syntax: str .isdigit () Example: strdigitdemo.py str1 = "12345" str2 = "python123" str3 = "123-45-78" str4 = "IIIV" str5 = "/u00B23" # 23 str6 = "/u00BD" # 1/2 print (str1.isdigit()) print (str2.isdigit()) print (str3.isdigit()) print (str4.isdigit()) print (str5.isdigit()) print (str6.isdigit()) Output: python strdigitldemo.py True False False False True False

String Functions & Methods in Python Cont.. ☞ isnumeric(): In python, isnumeric() method checks whether all the characters of the string are numeric characters or not. It returns True if all the characters are numeric, otherwise returns False. Syntax: str . isnumeric() Example: strnumericdemo.py str1 = "12345" str2 = "python123" str3 = "123-45-78" str4 = "IIIV" str5 = "/u00B23" # 23 str6 = "/u00BD" # 1/2 print (str1. isnumeric ()) print (str2. isnumeric ()) print (str3. isnumeric ()) print (str4. isnumeric ()) print (str5. isnumeric ()) print (str6. isnumeric ()) Output: python strnumericldemo.py True False False False True True

String Functions & Methods in Python Cont.. ☞ islower(): In python, islower() method returns True if all characters in the string are in lowercase. It returns False if not in lowercase. Syntax: str .islower () Example: strlowerdemo.py str1 = "python" str2= " PytHOn " str3= "python3.7.3" print (str1.islower()) print (str2.islower()) print (str3.islower()) Output: python strlowerldemo.py True False True

String Functions & Methods in Python Cont.. ☞ isupper(): In python string isupper() method returns True if all characters in the string are in uppercase. It returns False if not in uppercase. Syntax: str .isupper () Example: strupperdemo.py str1 = "PYTHON" str2= " PytHOn " str3= "PYTHON 3.7.3" print (str1.isupper()) print (str2.isupper()) print (str3.isupper()) Output: python strupperldemo.py True False True