ED5340 - Data Science: Theory at h y
and Practise a p
gan
th u
M u
a n
th
L3 - Strings m a n a
R a
Ramanathan Muthuganapathy (https://ed.iitm.ac.in/~raman)
Course web page: https://ed.iitm.ac.in/~raman/datascience.html
Moodle page: Available at https://courses.iitm.ac.in/
String a set of Unicode characters
• Representation of Strings
h y
at
• Accessing string elements an
a p
u g
uth
• String properties (including immutability)
th a n M
n a
m a
• String operations R a
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
String representation
• Single, double and triple quotes can be used
h y
at
• Raw string an
a p
u g
uth
• multi-line strings th a n M
n a
m a
R a
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
y
Demo using
at h
a p
gan
th u
u
L3_String_rep.py
n M
th a
n a
m a
R a
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Accessing string elements
arrays come into picture
• Str1 = “Raman”
h y
start 0 1 2 pat 3 4 end
a
gan
th u
M u
a n
R a thm a n
ana
m
Ra
start -5 -4 -3 -2 -1 end
• use str1[i] to access the characters in the string: str1[0] is R, str1[-1] is n and
so on.
• -0 and 0 are same indices!
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
String slicing
you can derive substrings directly
• str1[start : end] - extracts from start to end-1
h y
at
• str1[start : ] - extract from start to end an
a p
u g
u th
• str1[ :end] - extract from start to end
th a n- 1M
n a
a
m
• str1[-start :] - extract from a
-start
R to end
• str1[:-end] - extract from beginning to -end-1
Observation - The string starts from the first index (or beginning) but ends at
end - 1 if there is an `end’ index
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
String properties - immutable
• mutable ——> changeable
h y
at
• immutable ——> not changeable an
a p
u g
uth
• A string is not mutable th a n M
n a
m a
• str1 = “Kite” R a
• str1[0] = ‘L’ # Error - string is not mutable, characters in a string cannot be
changed
• str1 = “Lite” #This is fine…
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
y
Demo using at h
a p
gan
th u
L3_String_acces_slice.py
ath a n M u
L3_String_immutable.py
a n
a m
R
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Other properties of strings
• String is an object
h y
at
• Strings can be concatenated an
a p
u g
uth
• Strings can be replicated th a n M
n a
a m
• using ‘in’ for ‘containment’ a
checking
R
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
y
Demo using
at h
a p
gan
th u
u
L3_String_properties.py
n M
th a
n a
m a
R a
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
String operations
• syntax - string.function( ) #as string is an object and its member function can
be called using function( ) h y
p at
a
an
• Content test functions #isalpha( ), #isdigit u th u g( ) …..
n M
th a
• a
conversion functions #upper( ),malower(
n )…..
Ra
• search and replace #find( ), #replace( )
• str( )
• chr( ) and ord( ) functions
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
y
Demo using
at h
a p
gan
th u
u
L3_String_functions.py
n M
th a
n a
m a
R a
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
• Q1: WAP to find out the working other functions: len( ), rstrip( ), partition( ), str(
), chr( ), ord( ), index( ) etc.’? h y
p at
a
an
• Q2: Given a string, WAP to split them at u ththe
u g following: \, \\, blank space
n M
th a
• Q3: What is the difference between
a m a n a split( ) and partition( ) functions?
R
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Console input and output
Input
• Input from the keyboard - it’s a string
h y
at
• use the input( ) function to get from keyboard
an
a p
u g
uth
• use split( ) to split the input valuesthan M
n a
m a
• use type conversion functionsa
R such as int( ), float( ) etc.
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
y
Demo using
at h
a p
gan
th u
u
L3_input.py
n M
th a
n a
m a
R a
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Output statement
Standard print s
• Default print st. -
h y
at
• syntax - print(objects, sep = ‘ ’, end = ‘\n’, afile
n a p =sys.stdout, flush=false)
u g
uth
• objects - can be of any datatype (will
th a n be converted to string before print)
M
n a
a
m
• sep - separator symbol a
between
R various objects. Default is one blank space.
• end - specify what to print at the end. Default is ‘\n’
• file - Where to write. Default is sys.stdout (output screen)
• flush - output is flushed (True) or buffered (false)
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Output statement
Formatted print
• Using formatted string literals
h y
at
• Using the format( ) method gan
a p
th u
M u
a n
ath
a n
a m
R
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
h y
pat
a
an
Demo using L3_output.py
u g
uth
n M
th a
n a
m a
R a
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras