SlideShare a Scribd company logo
V.M.Prabhakaran,
Department Of Cse,
KIT- Coimbatore
Problem Solving and Python Programming 1
Strings: string slices, immutability,
string functions and methods, string
module
Problem Solving and Python Programming 2
Strings
• String is a sequence of characters.
• String may contain alphabets, numbers and
special characters.
• Usually strings are enclosed within a single
quotes and double quotes.
• Strings is immutable in nature.
• Example:
a=„hello world‟
b=“Python”
Problem Solving and Python Programming 3
Inbuilt String functions
• Python mainly contains 3 inbuilt string functions.
• They are
– len()
– max()
– min()
• len()- Find out the length of characters in string
• min()- Smallest value in a string based on ASCII values
• max()- Largest value in a string based on ASCII values
Problem Solving and Python Programming 4
What is ASCII values
Problem Solving and Python Programming 5
Example for Inbuilt string functions
name=input("Enter Your name:")
print("Welcome",name)
print("Length of your name:",len(name))
print("Maximum value of chararacter in your name",
max(name))
print("Minimum value of character in your name",min(name))
Problem Solving and Python Programming 6
OUTPUT
Enter Your name:PRABHAKARAN
Welcome PRABHAKARAN
Length of your name: 11
Maximum value of chararacter in your name R
Minimum value of character in your name A
Problem Solving and Python Programming 7
Strings Concatenation
• The + operator used for string concatenation.
Example:
a=“Hai”
b=“how are you”
c=a+b
print(c)
Problem Solving and Python Programming 8
Operators on String
• The Concatenate strings with the “*” operator
can create multiple concatenated copies.
• Example:
>>> print("Python"*10)
PythonPythonPythonPythonPythonPython
PythonPythonPythonPython
Problem Solving and Python Programming 9
String Slicing
• Slicing operation is used to return/select/slice
the particular substring based on user
requirements.
• A segment of string is called slice.
• Syntax: string_variablename [ start:end]
Problem Solving and Python Programming 10
String Slice example
s=“Hello”
Problem Solving and Python Programming 11
H e l l o
0 1 2 3 4
-5 -4 -3 -2 -1
Strings are immutable
• Strings are immutable character sets.
• Once a string is generated, you cannot change
any character within the string.
Problem Solving and Python Programming 12
String Comparision
• We can compare two strings using comparision
operators such as ==, !=, <,<=,>, >=
• Python compares strings based on their
corresponding ASCII values.
Problem Solving and Python Programming 13
Example of string comparision
str1="green"
str2="black"
print("Is both Equal:", str1==str2)
print("Is str1> str2:", str1>str2)
print("Is str1< str2:", str1<str2)
Problem Solving and Python Programming 14
OUTPUT:
Is both Equal: False
Is str1> str2: True
Is str1< str2: False
String formatting operator
• String formatting operator % is unique to
strings.
• Example:
print("My name is %s and i secured %d
marks in python” % ("Arbaz",92))
• Output:
My name is Arbaz and i secured 92 marks in
python
Problem Solving and Python Programming 15
String functions and methods
len() min() max() isalnum() isalpha()
isdigit() islower() isuppe() isspace() isidentifier()
endswith() startswith() find() count() capitalize()
title() lower() upper() swapcase() replace()
center() ljust() rjust() center() isstrip()
rstrip() strip()
Problem Solving and Python Programming 16
i) Converting string functions
Problem Solving and Python Programming 17
captitalize() Only First character capitalized
lower() All character converted to lowercase
upper() All character converted to uppercase
title() First character capitalized in each word
swapcase() Lower case letters are converted to
Uppercase and Uppercase letters are
converted to Lowercase
replace(old,new) Replaces old string with nre string
Program:
str=input("Enter any string:")
print("String Capitalized:", str.capitalize())
print("String lower case:", str.lower())
print("String upper case:", str.upper())
print("String title case:", str.title())
print("String swap case:", str.swapcase())
print("String replace case:",str.replace("python","python programming"))
Problem Solving and Python Programming 18
Output:
Enter any string: Welcome to python
String Capitalized: Welcome to python
String lower case: welcome to python
String upper case: WELCOME TO PYTHON
String title case: Welcome To Python
String swap case: wELCOME TO PYTHON
String replace case: Welcome to python programming
ii)Formatting String functions
Problem Solving and Python Programming 19
center(width) Returns a string centered in a field of given width
ljust(width) Returns a string left justified in a field of given width
rjust(width) Returns a string right justified in a field of given width
format(items) Formats a string
Program:
a=input("Enter any string:")
print("Center alignment:", a.center(20))
print("Left alignment:", a.ljust(20))
print("Right alignment:", a.rjust(20))
Problem Solving and Python Programming 20
Output:
iii) Removing whitespace
characters
Problem Solving and Python Programming 21
lstrip()
Returns a string with
leading whitespace
characters removed
rstrip()
Returns a string with
trailing whitespace
characters removed
strip()
Returns a string with
leading and trailing
whitespace characters
removed
Program
a=input("Enter any string:")
print("Left space trim:",a.lstrip())
print("Right space trim:",a.rstrip())
print("Left and right trim:",a.strip())
Problem Solving and Python Programming 22
Output:
iv) Testing String/Character
Problem Solving and Python Programming 23
isalnum()
Returns true if all characters in string are alphanumeric and there is
atleast one character
isalpha()
Returns true if all characters in string are alphabetic
isdigit()
Returns true if string contains only number character
islower()
Returns true if all characters in string are lowercase letters
isupper()
Returns true if all characters in string are uppercase letters
isspace() Returns true if string contains only whitespace characters.
Program
a=input("Enter any string:")
print("Alphanumeric:",a.isalnum())
print("Alphabetic:",a.isalpha())
print("Digits:",a.isdigit())
print("Lowecase:",a.islower())
print("Upper:",a.isupper())
Problem Solving and Python Programming 24
Output:
v) Searching for substring
Problem Solving and Python Programming 25
Endswith() Returns true if the strings ends with the substring
Startswith()
Returns true if the strings starts with the substring
Find()
Returns the lowest index or -1 if substring not found
Count() Returns the number of occurrences of substring
Program
a=input("Enter any string:")
print("Is string ends with thon:", a.endswith("thon"))
print("Is string starts with good:", a.startswith("good"))
print("Find:", a.find("ython"))
print("Count:", a.count(“o"))
Problem Solving and Python Programming 26
Output:
Enter any string : welcome to python
Is string ends with thon: True
Is string starts with good: False
Find: 12
Count: 3
String Modules
• String module contains a number of functions to
process standard Python strings
• Mostly used string modules:
string.upper()
string.upper()
string.split()
string.join()
string.replace()
string.find()
string.count()
Problem Solving and Python Programming 27
Example
import string
text="Monty Python Flying Circus"
print("Upper:", string.upper(text))
print("Lower:", string.lower(text))
print("Split:", string.split(text))
print("Join:", string.join(string.split(test),"+"))
print("Replace:", string.replace(text,"Python", "Java"))
print("Find:", string.find(text,"Python"))
print("Count", string.count(text,"n"))
Problem Solving and Python Programming 28
Output
Problem Solving and Python Programming 29
Upper: “MONTY PYTHON FLYING CIRCUS”
Lower: “monty python flying circus”
Split: [„Monty‟, „Python‟, „Flying‟, „Circus‟]
Join : Monty+Python+Flying+Circus
Replace: Monty Java Flying Circus
Find: 7
Count: 3

More Related Content

What's hot

Python strings
Python stringsPython strings
Python strings
Mohammed Sikander
 
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
Sujith Kumar
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
Ashok Raj
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
 
Python tuple
Python   tuplePython   tuple
Python tuple
Mohammed Sikander
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
 
Arrays
ArraysArrays
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
VedaGayathri1
 
Operators in python
Operators in pythonOperators in python
Operators in python
Prabhakaran V M
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
nitamhaske
 
Python for loop
Python for loopPython for loop
Python for loop
Aishwarya Deshmukh
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
Mohammed Sikander
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 

What's hot (20)

Python strings
Python stringsPython strings
Python strings
 
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
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
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
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Arrays
ArraysArrays
Arrays
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Strings
StringsStrings
Strings
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Python for loop
Python for loopPython for loop
Python for loop
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Strings in C
Strings in CStrings in C
Strings in C
 
Structure in C
Structure in CStructure in C
Structure in C
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 

Similar to Strings in python

stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
paijitk
 
Python data handling
Python data handlingPython data handling
Python data handling
Prof. Dr. K. Adisesha
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
ssusere19c741
 
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
Tinku91
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
String in c programming
String in c programmingString in c programming
String in c programming
Devan Thakur
 
strings11.pdf
strings11.pdfstrings11.pdf
strings11.pdf
TARUNKUMAR845504
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
adityakumawat625
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
Muthuselvam RS
 
Python- strings
Python- stringsPython- strings
Python- strings
Krishna Nanda
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
Rasan Samarasinghe
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Python Strings Methods
Python Strings MethodsPython Strings Methods
Python Strings Methods
Mr Examples
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
Appili Vamsi Krishna
 
Python ppt
Python pptPython ppt
Python ppt
Anush verma
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptx
PadreBhoj
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
Intro C# Book
 

Similar to Strings in python (20)

stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
Python data handling
Python data handlingPython data handling
Python data handling
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-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
 
Ch09
Ch09Ch09
Ch09
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
String in c programming
String in c programmingString in c programming
String in c programming
 
strings11.pdf
strings11.pdfstrings11.pdf
strings11.pdf
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Python- strings
Python- stringsPython- strings
Python- strings
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Python Strings Methods
Python Strings MethodsPython Strings Methods
Python Strings Methods
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Ch2
Ch2Ch2
Ch2
 
Python ppt
Python pptPython ppt
Python ppt
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptx
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 

More from Prabhakaran V M

Algorithmic problem solving
Algorithmic problem solvingAlgorithmic problem solving
Algorithmic problem solving
Prabhakaran V M
 
Open mp directives
Open mp directivesOpen mp directives
Open mp directives
Prabhakaran V M
 
Xml schema
Xml schemaXml schema
Xml schema
Prabhakaran V M
 
Html 5
Html 5Html 5
Introduction to Multi-core Architectures
Introduction to Multi-core ArchitecturesIntroduction to Multi-core Architectures
Introduction to Multi-core Architectures
Prabhakaran V M
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Applets
AppletsApplets

More from Prabhakaran V M (7)

Algorithmic problem solving
Algorithmic problem solvingAlgorithmic problem solving
Algorithmic problem solving
 
Open mp directives
Open mp directivesOpen mp directives
Open mp directives
 
Xml schema
Xml schemaXml schema
Xml schema
 
Html 5
Html 5Html 5
Html 5
 
Introduction to Multi-core Architectures
Introduction to Multi-core ArchitecturesIntroduction to Multi-core Architectures
Introduction to Multi-core Architectures
 
Java threads
Java threadsJava threads
Java threads
 
Applets
AppletsApplets
Applets
 

Recently uploaded

Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
varshanayak241
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
XfilesPro
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
MayankTawar1
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 

Recently uploaded (20)

Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 

Strings in python

  • 1. V.M.Prabhakaran, Department Of Cse, KIT- Coimbatore Problem Solving and Python Programming 1
  • 2. Strings: string slices, immutability, string functions and methods, string module Problem Solving and Python Programming 2
  • 3. Strings • String is a sequence of characters. • String may contain alphabets, numbers and special characters. • Usually strings are enclosed within a single quotes and double quotes. • Strings is immutable in nature. • Example: a=„hello world‟ b=“Python” Problem Solving and Python Programming 3
  • 4. Inbuilt String functions • Python mainly contains 3 inbuilt string functions. • They are – len() – max() – min() • len()- Find out the length of characters in string • min()- Smallest value in a string based on ASCII values • max()- Largest value in a string based on ASCII values Problem Solving and Python Programming 4
  • 5. What is ASCII values Problem Solving and Python Programming 5
  • 6. Example for Inbuilt string functions name=input("Enter Your name:") print("Welcome",name) print("Length of your name:",len(name)) print("Maximum value of chararacter in your name", max(name)) print("Minimum value of character in your name",min(name)) Problem Solving and Python Programming 6
  • 7. OUTPUT Enter Your name:PRABHAKARAN Welcome PRABHAKARAN Length of your name: 11 Maximum value of chararacter in your name R Minimum value of character in your name A Problem Solving and Python Programming 7
  • 8. Strings Concatenation • The + operator used for string concatenation. Example: a=“Hai” b=“how are you” c=a+b print(c) Problem Solving and Python Programming 8
  • 9. Operators on String • The Concatenate strings with the “*” operator can create multiple concatenated copies. • Example: >>> print("Python"*10) PythonPythonPythonPythonPythonPython PythonPythonPythonPython Problem Solving and Python Programming 9
  • 10. String Slicing • Slicing operation is used to return/select/slice the particular substring based on user requirements. • A segment of string is called slice. • Syntax: string_variablename [ start:end] Problem Solving and Python Programming 10
  • 11. String Slice example s=“Hello” Problem Solving and Python Programming 11 H e l l o 0 1 2 3 4 -5 -4 -3 -2 -1
  • 12. Strings are immutable • Strings are immutable character sets. • Once a string is generated, you cannot change any character within the string. Problem Solving and Python Programming 12
  • 13. String Comparision • We can compare two strings using comparision operators such as ==, !=, <,<=,>, >= • Python compares strings based on their corresponding ASCII values. Problem Solving and Python Programming 13
  • 14. Example of string comparision str1="green" str2="black" print("Is both Equal:", str1==str2) print("Is str1> str2:", str1>str2) print("Is str1< str2:", str1<str2) Problem Solving and Python Programming 14 OUTPUT: Is both Equal: False Is str1> str2: True Is str1< str2: False
  • 15. String formatting operator • String formatting operator % is unique to strings. • Example: print("My name is %s and i secured %d marks in python” % ("Arbaz",92)) • Output: My name is Arbaz and i secured 92 marks in python Problem Solving and Python Programming 15
  • 16. String functions and methods len() min() max() isalnum() isalpha() isdigit() islower() isuppe() isspace() isidentifier() endswith() startswith() find() count() capitalize() title() lower() upper() swapcase() replace() center() ljust() rjust() center() isstrip() rstrip() strip() Problem Solving and Python Programming 16
  • 17. i) Converting string functions Problem Solving and Python Programming 17 captitalize() Only First character capitalized lower() All character converted to lowercase upper() All character converted to uppercase title() First character capitalized in each word swapcase() Lower case letters are converted to Uppercase and Uppercase letters are converted to Lowercase replace(old,new) Replaces old string with nre string
  • 18. Program: str=input("Enter any string:") print("String Capitalized:", str.capitalize()) print("String lower case:", str.lower()) print("String upper case:", str.upper()) print("String title case:", str.title()) print("String swap case:", str.swapcase()) print("String replace case:",str.replace("python","python programming")) Problem Solving and Python Programming 18 Output: Enter any string: Welcome to python String Capitalized: Welcome to python String lower case: welcome to python String upper case: WELCOME TO PYTHON String title case: Welcome To Python String swap case: wELCOME TO PYTHON String replace case: Welcome to python programming
  • 19. ii)Formatting String functions Problem Solving and Python Programming 19 center(width) Returns a string centered in a field of given width ljust(width) Returns a string left justified in a field of given width rjust(width) Returns a string right justified in a field of given width format(items) Formats a string
  • 20. Program: a=input("Enter any string:") print("Center alignment:", a.center(20)) print("Left alignment:", a.ljust(20)) print("Right alignment:", a.rjust(20)) Problem Solving and Python Programming 20 Output:
  • 21. iii) Removing whitespace characters Problem Solving and Python Programming 21 lstrip() Returns a string with leading whitespace characters removed rstrip() Returns a string with trailing whitespace characters removed strip() Returns a string with leading and trailing whitespace characters removed
  • 22. Program a=input("Enter any string:") print("Left space trim:",a.lstrip()) print("Right space trim:",a.rstrip()) print("Left and right trim:",a.strip()) Problem Solving and Python Programming 22 Output:
  • 23. iv) Testing String/Character Problem Solving and Python Programming 23 isalnum() Returns true if all characters in string are alphanumeric and there is atleast one character isalpha() Returns true if all characters in string are alphabetic isdigit() Returns true if string contains only number character islower() Returns true if all characters in string are lowercase letters isupper() Returns true if all characters in string are uppercase letters isspace() Returns true if string contains only whitespace characters.
  • 25. v) Searching for substring Problem Solving and Python Programming 25 Endswith() Returns true if the strings ends with the substring Startswith() Returns true if the strings starts with the substring Find() Returns the lowest index or -1 if substring not found Count() Returns the number of occurrences of substring
  • 26. Program a=input("Enter any string:") print("Is string ends with thon:", a.endswith("thon")) print("Is string starts with good:", a.startswith("good")) print("Find:", a.find("ython")) print("Count:", a.count(“o")) Problem Solving and Python Programming 26 Output: Enter any string : welcome to python Is string ends with thon: True Is string starts with good: False Find: 12 Count: 3
  • 27. String Modules • String module contains a number of functions to process standard Python strings • Mostly used string modules: string.upper() string.upper() string.split() string.join() string.replace() string.find() string.count() Problem Solving and Python Programming 27
  • 28. Example import string text="Monty Python Flying Circus" print("Upper:", string.upper(text)) print("Lower:", string.lower(text)) print("Split:", string.split(text)) print("Join:", string.join(string.split(test),"+")) print("Replace:", string.replace(text,"Python", "Java")) print("Find:", string.find(text,"Python")) print("Count", string.count(text,"n")) Problem Solving and Python Programming 28
  • 29. Output Problem Solving and Python Programming 29 Upper: “MONTY PYTHON FLYING CIRCUS” Lower: “monty python flying circus” Split: [„Monty‟, „Python‟, „Flying‟, „Circus‟] Join : Monty+Python+Flying+Circus Replace: Monty Java Flying Circus Find: 7 Count: 3