SlideShare ist ein Scribd-Unternehmen logo
1 von 29
DATA STRUCTURES IN PYTHON
Data Structures in Python :
Lists
Dictionary
Tuples
Sets
LISTS
 List is most versatile datatype available in Python, written as a list of comma-
separated values (items) between square brackets.
 Items in a list can be Heterogenous types(need not be of the same type).
 Lists are mutable
 Concatenation produces a new lists.
 Append function extends a list with a new value without changing it.
LISTS ARE MUTABLE
 A mutable object can be changed after it's created, and an immutable object
cannot be changed after its created.
 Examples of mutable objects are dictionary,lists etc
 Example of lists in mutable form is:
LIST FUNCTIONS
 list. append ( x) :Add an item to the end of the list.
 list. extend ( L): Extend the list by appending all the items in the given list.
 list. insert ( i, x): Insert an item at a given position.
 list. remove ( x) : Remove the first item
from the list whose value is x.
It gives an error if there is no such item.
LIST FUNCTIONS
 list. pop ( [i]): Remove the item at the given position in the list, and return it.
 list. clear ( ): Remove all items from the list. Equivalent to del a[:] .
Not supported in python 2.X.
 list.reverse(): Reverse a given list.
 list. index ( x): Return the index in the list of the first item whose value is x. It is an
error if there is no such item.
 list. count ( x): Return the number of times x appears in the list.
 list. sort ( ): Sort the items of the list in place.
USING LISTS AS STACKS
 We can also use lists as a stack in Python.
 Stack follows a property of last in first out(LIFO Rule).
 To add an item to the top of the stack, use append() .
 To retrieve an item from the top of the stack, use pop() without an explicit index.
 Example:
USING LISTS AS QUEUES
 Python also uses a list as a queue where the first element added is the first element
retrieved.
 Lists are not efficient when used as a queue.
 While appends and pops from the end of list are fast, doing inserts or pops from the
beginning of a list is slow.
 To implement a queue, use collections.deque which was designed to have fast
appends and pops from both ends.
 Example:
List Comprehensions
 List comprehensions is very similar to set theory or set builder form .
 List comprehensions provide a concise way to create a lists.
 Allows to build a new set from existing sets.
 Is an extension to the lists.
 Examples:
 Create a list of two tuples using list comprehension
 Flattens a list using a list comprehension using “for”.
VARIABLES
DICTIONARY
 Dictionary is defined as an unordered set of key: value pairs, with the
requirement that the keys are unique . 
 Dictionary is a Mutable datatype.
 Dictionaries are sometimes found in other languages as “associative memories”
or “associative arrays”.
 Dictionaries are indexed by keys, which can be any immutable type or could be
a string.
 A pair of braces creates an empty dictionary: {} , not [].
 INITIALISATION: example be
 Keys could be a string in dictionary. Example
DICTIONARY
 We can nest dictionaries. Example:
 Directly assign values to a dictionary.
 Dictionary comprehensions can be used to create dictionaries from arbitrary key
and value expressions:
 The dict() constructor builds dictionaries directly from sequences of key-value
pairs:
TUPLES
 Tuple is a sequence data type.
 Tuples are immutable, and usually contain a heterogeneous sequence of
elements.
 Simultaneous assignment is possible in tuples.
 In tuple, we can assign a tuple of value to a name.
TUPLES
 Tuples may be nested.
 A tuple consists of a number of values separated by commas.
 Extract positions in tuples using slices.
VARIABLES
SETS
 A set is an unordered collection with no duplicate elements.
 Set objects also support mathematical operations like union, intersection,
difference, and symmetric difference.
 Curly braces or the set() function can be used to create sets.
 Example:
 Creates an empty set using
 Set membership in sets
 We can convert a list into sets.
SETS OPERATIONS
 Union : union of two sets is done using “set1|set2”
 Intersection : intersection of two sets can be done using “set1&set2”
 Set difference: In this, elements present in set2 is not included in the resultant set.
It can be don using “set1-set2
 Exclusive or: The syntax for exclusive or is “set1^set2”
STRINGS
 String is defined as a sequence or list of characters.
 String is immutable in nature means once defined , they cannot be changed.
 str is the type for strings in python.
 Define strings using quotes (“ or ‘ or “““)
>>> st = “Hello World”
>>> st = ‘Hello World’
>>> st = “““This is a multi-line
string that uses triple quotes.”””
‘ ‘ can be used to escape quotes:
STRINGS
 The string is enclosed in double quotes, if the string contains a single quote
and no double quotes, otherwise it is enclosed in single quotes.
 The print() function produces a more readable output, by omitting the enclosing
quotes and by printing escaped and special characters:
 ‘n’ is used to place a string in newline.
STRINGS
 If you don’t want characters prefaced by  to be interpreted as special characters,
you can use raw strings by adding an r before the first quote:
 Two or more string literals (i.e. the ones enclosed between quotes) next to each
other are automatically concatenated.
 Attempting to use an index that is too large
result in an error.
ACCESSING A STRINGS
 To access substrings, use the square brackets for slicing along with the index or
indices to obtain your substring.
 Example −
POSITIVE AND NEGATIVE INDICES
OPERATIONS ON STRINGS
 CONCATENATION (+) :
 adds value on the either sde of operator.
 REPETITION (*) :
 creates new strings,concatenating multiple copies of the same strings.
 SLICE ([]) :
 gives the character from the given index
 RANGE SLICE ([ :]) :
 gives the character from the given range.
OPERATIONS ON STRINGS
 MEMBERSHIP (in) or (not in) : membership returns true or false if the character
exist in the given string.
 length (len): It is used to find the length of the string.
 upper() : upper() is used to uppercase the string .
 lower() : lower() operation is used to lowercase the string.
 THERE ARE MANY MORE OPERATIONS ON STRINGS
MODIFYING STRINGS
 Cannot update a string “in place” as strings are immutable in nature.
 Example:
 Instead ,use slices and concatenation for modification of strings:-
 Example :
SLICING IN STRINGS
 A slice is a segment of string.
 Slicing allows you to obtain substring
 Syntax: Str[start : end]
 Start: substring start from this element
 End: end of substring excluding the element at this index
 S[i:j] starts at s[i] and ends at s[j-1]
 S[:j] means start at s[0], so s[0:j]
 S[i:] means ends at s[len(s)-1]
STRING FORMATTING
 str.format () is used to format the string.
 Examples: Replace argument by position in message string.
 Replace argument by names in message string.
VARIABLES
REVERT BACK AT devashish19gupta@gmail.com
CONTRIBUTERS
• EXAMPLE 1:
• ‘3d’ describes how to display the value 6.
• ‘d ’ is a code that specifies that the value should be treated as an integer value
• ‘3’is the width of the area to show 6.
• EXAMPLE 2:
• ‘6.2f’ describes how to display the value 46.89
• ‘f’ is a code specifies that 46.829 should be treated as a floating point value
• ‘6’ shows the width of the area to show 46.89
• ‘.2’ shows number of digits to show after decimal point.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

List in Python
List in PythonList in Python
List in Python
 
Python list
Python listPython list
Python list
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Python set
Python setPython set
Python set
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlib
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 

Ähnlich wie Data Structures in Python

11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
ssuser8e50d8
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov
 
ppt notes python language operators and data
ppt notes python language operators and datappt notes python language operators and data
ppt notes python language operators and data
SukhpreetSingh519414
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
phanleson
 

Ähnlich wie Data Structures in Python (20)

Data structures in Python
Data structures in PythonData structures in Python
Data structures in Python
 
dataStructuresInPython.pptx
dataStructuresInPython.pptxdataStructuresInPython.pptx
dataStructuresInPython.pptx
 
Chapter - 2.pptx
Chapter - 2.pptxChapter - 2.pptx
Chapter - 2.pptx
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4
 
ppt notes python language operators and data
ppt notes python language operators and datappt notes python language operators and data
ppt notes python language operators and data
 
13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss
 
DATA TYPES IN PYTHON.pdf
DATA TYPES IN PYTHON.pdfDATA TYPES IN PYTHON.pdf
DATA TYPES IN PYTHON.pdf
 
Notes8
Notes8Notes8
Notes8
 
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptxPYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
 
Python ds
Python dsPython ds
Python ds
 
Data structures in python
Data structures in pythonData structures in python
Data structures in python
 
The Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxThe Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptx
 
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdfCOMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
 
Introduction To Programming with Python-3
Introduction To Programming with Python-3Introduction To Programming with Python-3
Introduction To Programming with Python-3
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
STRINGS IN PYTHON
STRINGS IN PYTHONSTRINGS IN PYTHON
STRINGS IN PYTHON
 
Python data type
Python data typePython data type
Python data type
 
Python Collections
Python CollectionsPython Collections
Python Collections
 

Mehr von Devashish Kumar (6)

Python: Data Visualisation
Python: Data  VisualisationPython: Data  Visualisation
Python: Data Visualisation
 
Pandas csv
Pandas csvPandas csv
Pandas csv
 
Data Analysis packages
Data Analysis packagesData Analysis packages
Data Analysis packages
 
Data Analysis in Python-NumPy
Data Analysis in Python-NumPyData Analysis in Python-NumPy
Data Analysis in Python-NumPy
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
 
Cloud Computing Introductory-1
Cloud Computing Introductory-1Cloud Computing Introductory-1
Cloud Computing Introductory-1
 

Kürzlich hochgeladen

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 

Kürzlich hochgeladen (20)

Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

Data Structures in Python

  • 1.
  • 2. DATA STRUCTURES IN PYTHON Data Structures in Python : Lists Dictionary Tuples Sets
  • 3. LISTS  List is most versatile datatype available in Python, written as a list of comma- separated values (items) between square brackets.  Items in a list can be Heterogenous types(need not be of the same type).  Lists are mutable  Concatenation produces a new lists.  Append function extends a list with a new value without changing it.
  • 4. LISTS ARE MUTABLE  A mutable object can be changed after it's created, and an immutable object cannot be changed after its created.  Examples of mutable objects are dictionary,lists etc  Example of lists in mutable form is:
  • 5. LIST FUNCTIONS  list. append ( x) :Add an item to the end of the list.  list. extend ( L): Extend the list by appending all the items in the given list.  list. insert ( i, x): Insert an item at a given position.  list. remove ( x) : Remove the first item from the list whose value is x. It gives an error if there is no such item.
  • 6. LIST FUNCTIONS  list. pop ( [i]): Remove the item at the given position in the list, and return it.  list. clear ( ): Remove all items from the list. Equivalent to del a[:] . Not supported in python 2.X.  list.reverse(): Reverse a given list.  list. index ( x): Return the index in the list of the first item whose value is x. It is an error if there is no such item.  list. count ( x): Return the number of times x appears in the list.  list. sort ( ): Sort the items of the list in place.
  • 7. USING LISTS AS STACKS  We can also use lists as a stack in Python.  Stack follows a property of last in first out(LIFO Rule).  To add an item to the top of the stack, use append() .  To retrieve an item from the top of the stack, use pop() without an explicit index.  Example:
  • 8. USING LISTS AS QUEUES  Python also uses a list as a queue where the first element added is the first element retrieved.  Lists are not efficient when used as a queue.  While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow.  To implement a queue, use collections.deque which was designed to have fast appends and pops from both ends.  Example:
  • 9. List Comprehensions  List comprehensions is very similar to set theory or set builder form .  List comprehensions provide a concise way to create a lists.  Allows to build a new set from existing sets.  Is an extension to the lists.  Examples:  Create a list of two tuples using list comprehension  Flattens a list using a list comprehension using “for”.
  • 11. DICTIONARY  Dictionary is defined as an unordered set of key: value pairs, with the requirement that the keys are unique .  Dictionary is a Mutable datatype.  Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”.  Dictionaries are indexed by keys, which can be any immutable type or could be a string.  A pair of braces creates an empty dictionary: {} , not [].  INITIALISATION: example be  Keys could be a string in dictionary. Example
  • 12. DICTIONARY  We can nest dictionaries. Example:  Directly assign values to a dictionary.  Dictionary comprehensions can be used to create dictionaries from arbitrary key and value expressions:  The dict() constructor builds dictionaries directly from sequences of key-value pairs:
  • 13. TUPLES  Tuple is a sequence data type.  Tuples are immutable, and usually contain a heterogeneous sequence of elements.  Simultaneous assignment is possible in tuples.  In tuple, we can assign a tuple of value to a name.
  • 14. TUPLES  Tuples may be nested.  A tuple consists of a number of values separated by commas.  Extract positions in tuples using slices.
  • 16. SETS  A set is an unordered collection with no duplicate elements.  Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.  Curly braces or the set() function can be used to create sets.  Example:  Creates an empty set using  Set membership in sets  We can convert a list into sets.
  • 17. SETS OPERATIONS  Union : union of two sets is done using “set1|set2”  Intersection : intersection of two sets can be done using “set1&set2”  Set difference: In this, elements present in set2 is not included in the resultant set. It can be don using “set1-set2  Exclusive or: The syntax for exclusive or is “set1^set2”
  • 18. STRINGS  String is defined as a sequence or list of characters.  String is immutable in nature means once defined , they cannot be changed.  str is the type for strings in python.  Define strings using quotes (“ or ‘ or “““) >>> st = “Hello World” >>> st = ‘Hello World’ >>> st = “““This is a multi-line string that uses triple quotes.””” ‘ ‘ can be used to escape quotes:
  • 19. STRINGS  The string is enclosed in double quotes, if the string contains a single quote and no double quotes, otherwise it is enclosed in single quotes.  The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters:  ‘n’ is used to place a string in newline.
  • 20. STRINGS  If you don’t want characters prefaced by to be interpreted as special characters, you can use raw strings by adding an r before the first quote:  Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.  Attempting to use an index that is too large result in an error.
  • 21. ACCESSING A STRINGS  To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring.  Example −
  • 23. OPERATIONS ON STRINGS  CONCATENATION (+) :  adds value on the either sde of operator.  REPETITION (*) :  creates new strings,concatenating multiple copies of the same strings.  SLICE ([]) :  gives the character from the given index  RANGE SLICE ([ :]) :  gives the character from the given range.
  • 24. OPERATIONS ON STRINGS  MEMBERSHIP (in) or (not in) : membership returns true or false if the character exist in the given string.  length (len): It is used to find the length of the string.  upper() : upper() is used to uppercase the string .  lower() : lower() operation is used to lowercase the string.  THERE ARE MANY MORE OPERATIONS ON STRINGS
  • 25. MODIFYING STRINGS  Cannot update a string “in place” as strings are immutable in nature.  Example:  Instead ,use slices and concatenation for modification of strings:-  Example :
  • 26. SLICING IN STRINGS  A slice is a segment of string.  Slicing allows you to obtain substring  Syntax: Str[start : end]  Start: substring start from this element  End: end of substring excluding the element at this index  S[i:j] starts at s[i] and ends at s[j-1]  S[:j] means start at s[0], so s[0:j]  S[i:] means ends at s[len(s)-1]
  • 27. STRING FORMATTING  str.format () is used to format the string.  Examples: Replace argument by position in message string.  Replace argument by names in message string.
  • 28. VARIABLES REVERT BACK AT devashish19gupta@gmail.com
  • 29. CONTRIBUTERS • EXAMPLE 1: • ‘3d’ describes how to display the value 6. • ‘d ’ is a code that specifies that the value should be treated as an integer value • ‘3’is the width of the area to show 6. • EXAMPLE 2: • ‘6.2f’ describes how to display the value 46.89 • ‘f’ is a code specifies that 46.829 should be treated as a floating point value • ‘6’ shows the width of the area to show 46.89 • ‘.2’ shows number of digits to show after decimal point.