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 (20)

Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Python basics
Python basicsPython basics
Python basics
 
Strings
StringsStrings
Strings
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Strings
StringsStrings
Strings
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in python
 
C string
C stringC string
C string
 
Strings in c
Strings in cStrings in c
Strings in c
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
List in Python
List in PythonList in Python
List in Python
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
python Function
python Function python Function
python Function
 
Strings
StringsStrings
Strings
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Tuples in Python
Tuples in PythonTuples in Python
Tuples in Python
 
String in c programming
String in c programmingString in c programming
String in c programming
 

Recently uploaded

GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 

Recently uploaded (20)

GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 

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'