SlideShare a Scribd company logo
1 of 11
M Vishnuvardhan
String
String is basically represents sequence of characters. Technically string is a
sequence of Unicode characters in Python. Strings are written in single or double
quotes. Strings in Python are immutable i.e., modifying the string is not possible
Eg: name='Python’ or name= "Python".
Python allows to create multi line strings using ‘’’ or “””
Eg: msg = """ Python is a Object oriented
Language ""“
: msg = ‘’’ Python is a Object oriented
Language ‘’’
M Vishnuvardhan
Accessing chars in String
Individual characters of a string can be accessed using indexing. Index is a
integer number representing the position of character in the string it starts from
0 and ends at n-1 (n-length)
Eg: name= "Python".
print(name[0]) #returns P
print(name[2]) #returns t
print(name[7]) # index error occurs
Note: Python allows negative indexing on strings. Negative index starts
at -1 and go p to –n.
print(name[-1)) #prints n
M Vishnuvardhan
String – Slicing
Slicing operator (:) is used to get range of indexes. When specifying a range, the
return value will be a new string with the specified items.
Syntax stringName [ start : end ] # start is inclusive and end is exclusive
Eg: name= "Programming"
name[2:5] # returns ogr
If start is skipped then returns items from beginning, If end is skipped then
returns items till end .Slicing allows negative indexing also
name[-6:-3] # returns amm
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
P r o g r a m m i n g
0 1 2 3 4 5 6 7 8 9 10
M Vishnuvardhan
String – Advanced Slicing
Syntax stringName [ start : end: step ]
# start is inclusive and end is exclusive and step is increment
Eg: name= "Programming«
[low:high:+ve] – normal order
name[2:10:2] # returns ormi
[high:low:-ve] – reverse order
name[10:2:-1] # returns gnimmarg
name[::1] # returns Programming
name[::-1] # returns gnimmargorP
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
P r o g r a m m i n g
0 1 2 3 4 5 6 7 8 9 10
M Vishnuvardhan
String – Basic Operations
Python Expression Results Description
len(“Python”) 6 Length
“Python” + “Program” “PythonProgram” Concatenation
Py* 4 “PyPyPy” Repetition
y in “Python” True Membership
for x in “Python”:
print x
P y t h o n Iteration
M Vishnuvardhan
String methods
Method Description
count() Returns the number of times a specified value occurs in a string
find()
Searches the string for a specified value and returns the position of
where it was found
format() Formats specified values in a string
index()
Searches the string for a specified value and returns the position of
where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
islower() Returns True if all characters in the string are lower case
M Vishnuvardhan
String methods
Method Description
isnumeric() Returns True if all characters in the string are numeric
isupper() Returns True if all characters in the string are upper case
join() Converts the elements of an iterable into a string
lower() Converts a string into lower case
replace()
Returns a string where a specified value is replaced with a specified
value
split() Splits the string at the specified separator, and returns a list
strip() Returns a trimmed version of the string
title() Converts the first character of each word to upper case
upper() Converts a string into upper case
M Vishnuvardhan
String Formatting
format() method allows you to format selected parts of a string. It takes the
passed arguments, formats them, and places them in the string where the
placeholders {} are:
Syntax: string.format(value1, value2...)
The placeholders can be identified using named indexes {price}, numbered
indexes {0}, or even empty placeholders {}.
Eg:
txt1 = "Python {name}, is {ver}".format(name = "Language", ver = 3.10)
txt2 = "Python {0}, is {1}".format("Language",3.10)
txt3 = "Python {}, is {}".format("Language",3.10)
M Vishnuvardhan
String Escape Sequences
Code Result
' Single Quote
 Backslash
n New Line
r Carriage Return
t Tab
b Backspace
f Form Feed
ooo Octal value
xhh Hex value
M Vishnuvardhan

More Related Content

Similar to Python Strings.pptx

Similar to Python Strings.pptx (20)

Python data handling
Python data handlingPython data handling
Python data handling
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
python_strings.pdf
python_strings.pdfpython_strings.pdf
python_strings.pdf
 
STRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptxSTRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptx
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
String notes
String notesString notes
String notes
 
Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )
 
Python Strings Methods
Python Strings MethodsPython Strings Methods
Python Strings Methods
 
STRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfSTRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdf
 
Raspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry PiRaspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry Pi
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptx
 
strings11.pdf
strings11.pdfstrings11.pdf
strings11.pdf
 
Chapter 11 Strings.pptx
Chapter 11 Strings.pptxChapter 11 Strings.pptx
Chapter 11 Strings.pptx
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 
unit 1.docx
unit 1.docxunit 1.docx
unit 1.docx
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
Detailed description of Strings in Python
Detailed description of Strings in PythonDetailed description of Strings in Python
Detailed description of Strings in Python
 

More from M Vishnuvardhan Reddy

More from M Vishnuvardhan Reddy (20)

Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
 
Lists_tuples.pptx
Lists_tuples.pptxLists_tuples.pptx
Lists_tuples.pptx
 
Python Control Structures.pptx
Python Control Structures.pptxPython Control Structures.pptx
Python Control Structures.pptx
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
 
Python Operators.pptx
Python Operators.pptxPython Operators.pptx
Python Operators.pptx
 
Python Datatypes.pptx
Python Datatypes.pptxPython Datatypes.pptx
Python Datatypes.pptx
 
DataScience.pptx
DataScience.pptxDataScience.pptx
DataScience.pptx
 
Html forms
Html formsHtml forms
Html forms
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Scanner class
Scanner classScanner class
Scanner class
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java intro
Java introJava intro
Java intro
 
Java applets
Java appletsJava applets
Java applets
 
Exception handling
Exception handling Exception handling
Exception handling
 
Control structures
Control structuresControl structures
Control structures
 
Constructors
ConstructorsConstructors
Constructors
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
 
Shell sort
Shell sortShell sort
Shell sort
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

Python Strings.pptx

  • 1.
  • 2. M Vishnuvardhan String String is basically represents sequence of characters. Technically string is a sequence of Unicode characters in Python. Strings are written in single or double quotes. Strings in Python are immutable i.e., modifying the string is not possible Eg: name='Python’ or name= "Python". Python allows to create multi line strings using ‘’’ or “”” Eg: msg = """ Python is a Object oriented Language ""“ : msg = ‘’’ Python is a Object oriented Language ‘’’
  • 3. M Vishnuvardhan Accessing chars in String Individual characters of a string can be accessed using indexing. Index is a integer number representing the position of character in the string it starts from 0 and ends at n-1 (n-length) Eg: name= "Python". print(name[0]) #returns P print(name[2]) #returns t print(name[7]) # index error occurs Note: Python allows negative indexing on strings. Negative index starts at -1 and go p to –n. print(name[-1)) #prints n
  • 4. M Vishnuvardhan String – Slicing Slicing operator (:) is used to get range of indexes. When specifying a range, the return value will be a new string with the specified items. Syntax stringName [ start : end ] # start is inclusive and end is exclusive Eg: name= "Programming" name[2:5] # returns ogr If start is skipped then returns items from beginning, If end is skipped then returns items till end .Slicing allows negative indexing also name[-6:-3] # returns amm -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 P r o g r a m m i n g 0 1 2 3 4 5 6 7 8 9 10
  • 5. M Vishnuvardhan String – Advanced Slicing Syntax stringName [ start : end: step ] # start is inclusive and end is exclusive and step is increment Eg: name= "Programming« [low:high:+ve] – normal order name[2:10:2] # returns ormi [high:low:-ve] – reverse order name[10:2:-1] # returns gnimmarg name[::1] # returns Programming name[::-1] # returns gnimmargorP -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 P r o g r a m m i n g 0 1 2 3 4 5 6 7 8 9 10
  • 6. M Vishnuvardhan String – Basic Operations Python Expression Results Description len(“Python”) 6 Length “Python” + “Program” “PythonProgram” Concatenation Py* 4 “PyPyPy” Repetition y in “Python” True Membership for x in “Python”: print x P y t h o n Iteration
  • 7. M Vishnuvardhan String methods Method Description count() Returns the number of times a specified value occurs in a string find() Searches the string for a specified value and returns the position of where it was found format() Formats specified values in a string index() Searches the string for a specified value and returns the position of where it was found isalnum() Returns True if all characters in the string are alphanumeric isalpha() Returns True if all characters in the string are in the alphabet isdecimal() Returns True if all characters in the string are decimals isdigit() Returns True if all characters in the string are digits islower() Returns True if all characters in the string are lower case
  • 8. M Vishnuvardhan String methods Method Description isnumeric() Returns True if all characters in the string are numeric isupper() Returns True if all characters in the string are upper case join() Converts the elements of an iterable into a string lower() Converts a string into lower case replace() Returns a string where a specified value is replaced with a specified value split() Splits the string at the specified separator, and returns a list strip() Returns a trimmed version of the string title() Converts the first character of each word to upper case upper() Converts a string into upper case
  • 9. M Vishnuvardhan String Formatting format() method allows you to format selected parts of a string. It takes the passed arguments, formats them, and places them in the string where the placeholders {} are: Syntax: string.format(value1, value2...) The placeholders can be identified using named indexes {price}, numbered indexes {0}, or even empty placeholders {}. Eg: txt1 = "Python {name}, is {ver}".format(name = "Language", ver = 3.10) txt2 = "Python {0}, is {1}".format("Language",3.10) txt3 = "Python {}, is {}".format("Language",3.10)
  • 10. M Vishnuvardhan String Escape Sequences Code Result ' Single Quote Backslash n New Line r Carriage Return t Tab b Backspace f Form Feed ooo Octal value xhh Hex value