SlideShare a Scribd company logo
1 of 15
G H PATEL COLLEGE OF ENGINEERING &
TECHNOLOGY, V. V. NAGAR,
ACADEMIC YEAR: 2019-20 (EVEN SEM)
CLASS: FINAL YEAR COMPUTER ENGINEERING
2180711-PYTHON PROGRAMMING
BY:
160110107031, NARSINGANI AMISHA
BATCH: 1B07
Presentation Topic:
Strings in Python
WHAT ARE STRINGS?
• STRINGS ARE ARRAYS OF BYTES REPRESENTING UNICODE CHARACTERS.
• PYTHON DOES NOT HAVE A CHARACTER DATA TYPE, A SINGLE
CHARACTER IS SIMPLY A STRING WITH A LENGTH OF 1.
• SQUARE BRACKETS CAN BE USED TO ACCESS ELEMENTS OF THE STRING.
• STRINGS IN PYTHON CAN BE CREATED USING SINGLE QUOTES OR DOUBLE
QUOTES OR EVEN TRIPLE QUOTES.
• FOR EXAMPLE:
STRING1 = ‘WELCOME TO THE GCET WORLD’
STRING2 = “WELCOME EVERYONE!”
STRING 3 = '''I'M A GEEK AND I LIVE IN A WORLD OF "GEEKS"'''
ITERATING THROUGH STRINGS
• USING FOR LOOP WE CAN ITERATE THROUGH A STRING.
ACCESSING INDIVIDUAL CHARACTERS
• WE CAN ACCESS INDIVIDUAL CHARACTERS OF STRING USING INDEXING.
• INDEXING ALLOWS NEGATIVE ADDRESS REFERENCES TO ACCESS
CHARACTERS FROM THE BACK OF THE STRING, E.G. -1 REFERS TO THE
LAST CHARACTER, -2 REFERS TO THE SECOND LAST CHARACTER AND SO
ON. (SEE THE BELOW FIGURE)
• WHILE ACCESSING AN INDEX OUT OF THE RANGE WILL CAUSE
AN INDEXERROR.
P R O G R A M M I N G
0 1 2 3 4 5 6 7 8 9 10
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
ACCESSING INDIVIDUAL CHARACTERS
STRING1=“PROGRAMMING”
PRINT("INITIAL STRING: ")
PRINT(STRING1)
PRINT("NFIRST CHARACTER OF STRING IS: ")
PRINT(STRING1[0]) #OUTPUT: P
PRINT("NLAST CHARACTER OF STRING IS: ")
PRINT(STRING1[-1]) #OUTPUT: G
STRING SLICING
• TO ACCESS A RANGE OF CHARACTERS IN THE STRING, METHOD OF SLICING IS
USED.
• SLICING IN A STRING IS DONE BY USING A SLICING OPERATOR (COLON).
• WE CAN ACCESS SUBSTRINGS USING SLICING
• FOR EXAMPLE:
C=“PROGRAMMING IS GOOD“
PRINT(C)
D=C[5:7]
PRINT(D) #OUTPUT: AM
E=C[12:14]
PRINT(E) #OUTPUT: IS
STRING CONCATENATION
• JOINING OF TWO OR MORE STRINGS INTO A SINGLE ONE IS CALLED
CONCATENATION.
• THE + OPERATOR DOES THIS IN PYTHON.
• THE * OPERATOR CAN BE USED TO REPEAT THE STRING FOR A GIVEN NUMBER OF
TIMES.
• FOR EXAMPLE:
firstname = “Amisha”
lastname = “Narsingani”
fullname=firstname +
lastname
print(fullname)
word=“hello “
echo = word*3
print(echo)
STRINGS ARE IMMUTABLE
• UPDATION OR DELETION OF CHARACTERS FROM A STRING IS NOT
ALLOWED.
• THIS WILL CAUSE AN ERROR BECAUSE ITEM ASSIGNMENT OR ITEM
DELETION FROM A STRING IS NOT SUPPORTED.
• ALTHOUGH DELETION OF ENTIRE STRING IS POSSIBLE WITH THE USE OF A
BUILT-IN DEL KEYWORD.
• THIS IS BECAUSE STRINGS ARE IMMUTABLE, HENCE ELEMENTS OF A STRING
CANNOT BE CHANGED ONCE IT HAS BEEN ASSIGNED. ONLY NEW STRINGS
CAN BE REASSIGNED TO THE SAME NAME.
STRINGS ARE IMMUTABLE
Updating a character:
STRING1 = "HELLO, I'M A GEEK"
PRINT(STRING1)
# UPDATING A CHARACTER OF THE STRING
STRING1[2] = 'P'
PRINT("NUPDATING CHARACTER AT 2ND INDEX: ")
PRINT(STRING1)
#TYPEERROR: ‘STR’ OBJECT DOES NOT SUPPORT ITEM
ASSIGNMENT
DEL STRING1[2]
PRINT("NDELETING CHARACTER AT 2ND INDEX: ")
PRINT(STRING1)
#TYPEERROR: ‘STR’ OBJECT DOESN’T SUPPORT ITEM
DELETION
Updating Entire String:
String1 = "Hello, I'm a Geek"
print(String1)
# Updating a String
String1 = "Welcome to the Geek World"
print("nUpdated String: ")
print(String1)
#doesn’t generate an error and gives output
successfully
del String1
#successfully deletes String1
SPLIT METHOD
• RETURNALIST OF THE WORDS IN STRING
SPLIT SEPARATOR
• DEFAULT SEPARATOR IS ANY SPACE(SPACE,NEWLINE,TAB)
• TO SPECIFY ANY OTHER SEPARATOR, SPECIFY IT EXPLICITLY.
JOIN METHOD
• USED TO EFFICIENTLY CONSTRUCT STRINGS FROM MULTIPLE FRAGMENTS.
STR.JOIN(ITERABLE)
• RETURN A STRING WHICH IS THE CONCATENATION OF THE STRINGS IN ITERABLE.
• A TYPEERROR WILL BE RAISED IF THERE ARE ANY NON-STRING VALUES IN
ITERABLE.
• FOR EXAMPLE:
vowels = ['a', 'e', 'i', 'o',
'u’]
print(vowels)
['a', 'e', 'i', 'o', 'u’]
s = "-".join(vowels)
print(s)
'a-e-i-o-u'
STRINGMETHODS
String methods
Thank You!

More Related Content

What's hot

What's hot (20)

Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
 
Function in Python
Function in PythonFunction in Python
Function in Python
 
Function arguments In Python
Function arguments In PythonFunction arguments In Python
Function arguments In Python
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Tuple in python
Tuple in pythonTuple in python
Tuple in python
 
Python Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String MethodsPython Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String Methods
 
Python set
Python setPython set
Python set
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Tuples in Python
Tuples in PythonTuples in Python
Tuples in Python
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
List in Python
List in PythonList in Python
List in Python
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 

Recently uploaded

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
 
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 - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 

Recently uploaded (20)

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
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
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, ...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
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
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
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
 
(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...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
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
 
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
 
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)
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 

Strings in Python

  • 1. G H PATEL COLLEGE OF ENGINEERING & TECHNOLOGY, V. V. NAGAR, ACADEMIC YEAR: 2019-20 (EVEN SEM) CLASS: FINAL YEAR COMPUTER ENGINEERING 2180711-PYTHON PROGRAMMING BY: 160110107031, NARSINGANI AMISHA BATCH: 1B07 Presentation Topic: Strings in Python
  • 2. WHAT ARE STRINGS? • STRINGS ARE ARRAYS OF BYTES REPRESENTING UNICODE CHARACTERS. • PYTHON DOES NOT HAVE A CHARACTER DATA TYPE, A SINGLE CHARACTER IS SIMPLY A STRING WITH A LENGTH OF 1. • SQUARE BRACKETS CAN BE USED TO ACCESS ELEMENTS OF THE STRING. • STRINGS IN PYTHON CAN BE CREATED USING SINGLE QUOTES OR DOUBLE QUOTES OR EVEN TRIPLE QUOTES. • FOR EXAMPLE: STRING1 = ‘WELCOME TO THE GCET WORLD’ STRING2 = “WELCOME EVERYONE!” STRING 3 = '''I'M A GEEK AND I LIVE IN A WORLD OF "GEEKS"'''
  • 3. ITERATING THROUGH STRINGS • USING FOR LOOP WE CAN ITERATE THROUGH A STRING.
  • 4. ACCESSING INDIVIDUAL CHARACTERS • WE CAN ACCESS INDIVIDUAL CHARACTERS OF STRING USING INDEXING. • INDEXING ALLOWS NEGATIVE ADDRESS REFERENCES TO ACCESS CHARACTERS FROM THE BACK OF THE STRING, E.G. -1 REFERS TO THE LAST CHARACTER, -2 REFERS TO THE SECOND LAST CHARACTER AND SO ON. (SEE THE BELOW FIGURE) • WHILE ACCESSING AN INDEX OUT OF THE RANGE WILL CAUSE AN INDEXERROR. P R O G R A M M I N G 0 1 2 3 4 5 6 7 8 9 10 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
  • 5. ACCESSING INDIVIDUAL CHARACTERS STRING1=“PROGRAMMING” PRINT("INITIAL STRING: ") PRINT(STRING1) PRINT("NFIRST CHARACTER OF STRING IS: ") PRINT(STRING1[0]) #OUTPUT: P PRINT("NLAST CHARACTER OF STRING IS: ") PRINT(STRING1[-1]) #OUTPUT: G
  • 6. STRING SLICING • TO ACCESS A RANGE OF CHARACTERS IN THE STRING, METHOD OF SLICING IS USED. • SLICING IN A STRING IS DONE BY USING A SLICING OPERATOR (COLON). • WE CAN ACCESS SUBSTRINGS USING SLICING • FOR EXAMPLE: C=“PROGRAMMING IS GOOD“ PRINT(C) D=C[5:7] PRINT(D) #OUTPUT: AM E=C[12:14] PRINT(E) #OUTPUT: IS
  • 7. STRING CONCATENATION • JOINING OF TWO OR MORE STRINGS INTO A SINGLE ONE IS CALLED CONCATENATION. • THE + OPERATOR DOES THIS IN PYTHON. • THE * OPERATOR CAN BE USED TO REPEAT THE STRING FOR A GIVEN NUMBER OF TIMES. • FOR EXAMPLE: firstname = “Amisha” lastname = “Narsingani” fullname=firstname + lastname print(fullname) word=“hello “ echo = word*3 print(echo)
  • 8. STRINGS ARE IMMUTABLE • UPDATION OR DELETION OF CHARACTERS FROM A STRING IS NOT ALLOWED. • THIS WILL CAUSE AN ERROR BECAUSE ITEM ASSIGNMENT OR ITEM DELETION FROM A STRING IS NOT SUPPORTED. • ALTHOUGH DELETION OF ENTIRE STRING IS POSSIBLE WITH THE USE OF A BUILT-IN DEL KEYWORD. • THIS IS BECAUSE STRINGS ARE IMMUTABLE, HENCE ELEMENTS OF A STRING CANNOT BE CHANGED ONCE IT HAS BEEN ASSIGNED. ONLY NEW STRINGS CAN BE REASSIGNED TO THE SAME NAME.
  • 9. STRINGS ARE IMMUTABLE Updating a character: STRING1 = "HELLO, I'M A GEEK" PRINT(STRING1) # UPDATING A CHARACTER OF THE STRING STRING1[2] = 'P' PRINT("NUPDATING CHARACTER AT 2ND INDEX: ") PRINT(STRING1) #TYPEERROR: ‘STR’ OBJECT DOES NOT SUPPORT ITEM ASSIGNMENT DEL STRING1[2] PRINT("NDELETING CHARACTER AT 2ND INDEX: ") PRINT(STRING1) #TYPEERROR: ‘STR’ OBJECT DOESN’T SUPPORT ITEM DELETION Updating Entire String: String1 = "Hello, I'm a Geek" print(String1) # Updating a String String1 = "Welcome to the Geek World" print("nUpdated String: ") print(String1) #doesn’t generate an error and gives output successfully del String1 #successfully deletes String1
  • 10. SPLIT METHOD • RETURNALIST OF THE WORDS IN STRING
  • 11. SPLIT SEPARATOR • DEFAULT SEPARATOR IS ANY SPACE(SPACE,NEWLINE,TAB) • TO SPECIFY ANY OTHER SEPARATOR, SPECIFY IT EXPLICITLY.
  • 12. JOIN METHOD • USED TO EFFICIENTLY CONSTRUCT STRINGS FROM MULTIPLE FRAGMENTS. STR.JOIN(ITERABLE) • RETURN A STRING WHICH IS THE CONCATENATION OF THE STRINGS IN ITERABLE. • A TYPEERROR WILL BE RAISED IF THERE ARE ANY NON-STRING VALUES IN ITERABLE. • FOR EXAMPLE: vowels = ['a', 'e', 'i', 'o', 'u’] print(vowels) ['a', 'e', 'i', 'o', 'u’] s = "-".join(vowels) print(s) 'a-e-i-o-u'